<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1"><meta name="format-detection" content="telephone=no"><meta name="apple-mobile-web-app-capable" content="yes"><meta name="apple-mobile-web-app-status-bar-style" content="black"><link rel="icon" href="/images/icons/favicon-16x16.png?v=2.7.0" type="image/png" sizes="16x16"><link rel="icon" href="/images/icons/favicon-32x32.png?v=2.7.0" type="image/png" sizes="32x32"><meta name="baidu-site-verification" content="code-aMPs2J7ZPy"><meta name="description" content="1.Collection集合                           1.1数组和集合的区别        相同点：都是容器,可以存储多个数据  不同点：  数组的长度是不可变的,集合的长度是可变的 数组可以存基本数据类型和引用数据类型 集合只能存引用数据类型,如果要存基本数据类型,需要存对应的包装类">
<meta property="og:type" content="article">
<meta property="og:title" content="4.集合">
<meta property="og:url" content="http://bujiuzhi.gitee.io/2022/02/16/4.%E9%9B%86%E5%90%88/index.html">
<meta property="og:site_name" content="星辰大海">
<meta property="og:description" content="1.Collection集合                           1.1数组和集合的区别        相同点：都是容器,可以存储多个数据  不同点：  数组的长度是不可变的,集合的长度是可变的 数组可以存基本数据类型和引用数据类型 集合只能存引用数据类型,如果要存基本数据类型,需要存对应的包装类">
<meta property="og:locale" content="en_US">
<meta property="og:image" content="https://s2.loli.net/2022/02/16/kpsH1biCDzERfZo.png">
<meta property="og:image" content="https://s2.loli.net/2022/02/16/ohlkCrNfOveXpLD.png">
<meta property="og:image" content="https://s2.loli.net/2022/02/16/ECLbPKZOpUGig8c.png">
<meta property="article:published_time" content="2022-02-16T08:48:26.000Z">
<meta property="article:modified_time" content="2022-09-13T16:30:11.000Z">
<meta property="article:author" content="不久">
<meta property="article:tag" content="学习笔记">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://s2.loli.net/2022/02/16/kpsH1biCDzERfZo.png"><title>4.集合 | 星辰大海</title><link ref="canonical" href="http://bujiuzhi.gitee.io/2022/02/16/4.%E9%9B%86%E5%90%88/"><link rel="dns-prefetch" href="https://cdn.jsdelivr.net"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.12.1/css/all.min.css" type="text/css"><link rel="stylesheet" href="/css/index.css?v=2.7.0"><link rel="dns-prefetch" href="https://hm.baidu.com"><script>var _hmt = _hmt || [];
(function() {
  var hm = document.createElement('script');
  hm.src = 'https://hm.baidu.com/hm.js?12b4347d0a1f32de969a2276a08cb466';
  hm.async = true;

  if (false) {
    hm.setAttribute('data-pjax', '');
  }
  var s = document.getElementsByTagName('script')[0]; 
  s.parentNode.insertBefore(hm, s);
})();</script><script>var Stun = window.Stun || {};
var CONFIG = {
  root: '/',
  algolia: undefined,
  assistSearch: undefined,
  fontIcon: {"prompt":{"success":"fas fa-check-circle","info":"fas fa-arrow-circle-right","warning":"fas fa-exclamation-circle","error":"fas fa-times-circle"},"copyBtn":"fas fa-copy"},
  sidebar: {"offsetTop":"20px","tocMaxDepth":8},
  header: {"enable":true,"showOnPost":false,"scrollDownIcon":false},
  postWidget: {"endText":true},
  nightMode: {"enable":true},
  back2top: {"enable":true},
  codeblock: {"style":"carbon","highlight":"light","wordWrap":false},
  reward: true,
  fancybox: false,
  zoomImage: {"gapAside":"20px"},
  galleryWaterfall: undefined,
  lazyload: false,
  pjax: undefined,
  externalLink: {"icon":{"enable":true,"name":"fas fa-external-link-alt"}},
  shortcuts: undefined,
  prompt: {"copyButton":"Copy","copySuccess":"Copy Success","copyError":"Copy Error"},
  sourcePath: {"js":"js","css":"css","images":"images"},
};

window.CONFIG = CONFIG;</script><meta name="generator" content="Hexo 6.3.0"></head><body><div class="container" id="container"><header class="header" id="header"><div class="header-inner header-inner--height header-inner--bgcolor"><nav class="header-nav header-nav--sticky"><div class="header-nav-inner"><div class="header-nav-menubtn"><i class="fas fa-bars"></i></div><div class="header-nav-menu"><div class="header-nav-menu-item"><a class="header-nav-menu-item__link" href="/"><span class="header-nav-menu-item__icon"><i class="fas fa-home"></i></span><span class="header-nav-menu-item__text">Home</span></a></div><div class="header-nav-menu-item"><a class="header-nav-menu-item__link" href="/archives/"><span class="header-nav-menu-item__icon"><i class="fas fa-folder-open"></i></span><span class="header-nav-menu-item__text">Archives</span></a></div><div class="header-nav-menu-item"><a class="header-nav-menu-item__link" href="/categories/"><span class="header-nav-menu-item__icon"><i class="fas fa-layer-group"></i></span><span class="header-nav-menu-item__text">Categories</span></a></div><div class="header-nav-menu-item"><a class="header-nav-menu-item__link" href="/tags/"><span class="header-nav-menu-item__icon"><i class="fas fa-tags"></i></span><span class="header-nav-menu-item__text">Tags</span></a></div></div><div class="header-nav-mode"><div class="mode"><div class="mode-track"><span class="mode-track-moon"></span><span class="mode-track-sun"></span></div><div class="mode-thumb"></div></div></div></div></nav></div></header><main class="main" id="main"><div class="main-inner"><div class="content-wrap" id="content-wrap"><div class="content" id="content"><!-- Just used to judge whether it is an article page--><div id="is-post"></div><div class="post"><header class="post-header"><h1 class="post-title">4.集合</h1><div class="post-meta"><span class="post-meta-item post-meta-item--createtime"><span class="post-meta-item__icon"><i class="far fa-calendar-plus"></i></span><span class="post-meta-item__info">Created</span><span class="post-meta-item__value">2022-02-16</span></span><span class="post-meta-item post-meta-item--updatetime"><span class="post-meta-item__icon"><i class="far fa-calendar-check"></i></span><span class="post-meta-item__info">Updated</span><span class="post-meta-item__value">2022-09-14</span></span></div></header><div class="post-body">
        <h1 id="1-Collection集合"   >
          <a href="#1-Collection集合" class="heading-link"><i class="fas fa-link"></i></a><a href="#1-Collection集合" class="headerlink" title="1.Collection集合"></a>1.Collection集合</h1>
      
        <h2 id="1-1数组和集合的区别"   >
          <a href="#1-1数组和集合的区别" class="heading-link"><i class="fas fa-link"></i></a><a href="#1-1数组和集合的区别" class="headerlink" title="1.1数组和集合的区别"></a>1.1数组和集合的区别</h2>
      <ul>
<li><p>相同点：都是容器,可以存储多个数据</p>
</li>
<li><p>不同点：</p>
<ol>
<li>数组的长度是不可变的,集合的长度是可变的</li>
<li>数组可以存基本数据类型和引用数据类型</li>
<li>集合只能存引用数据类型,如果要存基本数据类型,需要存对应的包装类</li>
</ol>
</li>
</ul>

        <h2 id="1-2-集合类体系结构"   >
          <a href="#1-2-集合类体系结构" class="heading-link"><i class="fas fa-link"></i></a><a href="#1-2-集合类体系结构" class="headerlink" title="1.2 集合类体系结构"></a>1.2 集合类体系结构</h2>
      <p><img src="https://s2.loli.net/2022/02/16/kpsH1biCDzERfZo.png" alt="01_集合类体系结构图"></p>

        <h2 id="1-3-概述和使用"   >
          <a href="#1-3-概述和使用" class="heading-link"><i class="fas fa-link"></i></a><a href="#1-3-概述和使用" class="headerlink" title="1.3 概述和使用"></a>1.3 概述和使用</h2>
      
        <h3 id="1-3-1-Collection集合概述"   >
          <a href="#1-3-1-Collection集合概述" class="heading-link"><i class="fas fa-link"></i></a><a href="#1-3-1-Collection集合概述" class="headerlink" title="1.3.1 Collection集合概述"></a>1.3.1 Collection集合概述</h3>
      <ul>
<li>是单例集合的顶层接口,它表示一组对象,这些对象也称为Collection的元素</li>
<li>JDK 不提供此接口的任何直接实现.它提供更具体的子接口(如Set和List)实现</li>
</ul>

        <h3 id="1-3-2-创建Collection集合的对象"   >
          <a href="#1-3-2-创建Collection集合的对象" class="heading-link"><i class="fas fa-link"></i></a><a href="#1-3-2-创建Collection集合的对象" class="headerlink" title="1.3.2 创建Collection集合的对象"></a>1.3.2 创建Collection集合的对象</h3>
      <ul>
<li>多态的方式</li>
<li>具体的实现类ArrayList</li>
</ul>

        <h3 id="1-3-3-Collection集合常用方法"   >
          <a href="#1-3-3-Collection集合常用方法" class="heading-link"><i class="fas fa-link"></i></a><a href="#1-3-3-Collection集合常用方法" class="headerlink" title="1.3.3 Collection集合常用方法"></a>1.3.3 Collection集合常用方法</h3>
      <figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">boolean</span> <span class="title function_">add</span><span class="params">(E e)</span>	<span class="comment">//添加元素</span></span><br><span class="line">  </span><br><span class="line"><span class="type">boolean</span> <span class="title function_">remove</span><span class="params">(Object o)</span>	<span class="comment">//从集合中移除指定的元素</span></span><br><span class="line">  </span><br><span class="line"><span class="type">boolean</span> <span class="title function_">removeIf</span><span class="params">(Object o)</span>	<span class="comment">//根据条件进行移除</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">void</span>   <span class="title function_">clear</span><span class="params">()</span>	<span class="comment">//清空集合中的元素</span></span><br><span class="line">  </span><br><span class="line"><span class="type">boolean</span> <span class="title function_">contains</span><span class="params">(Object o)</span>	<span class="comment">//判断集合中是否存在指定的元素</span></span><br><span class="line">  </span><br><span class="line"><span class="type">boolean</span> <span class="title function_">isEmpty</span><span class="params">()</span>	<span class="comment">//判断集合是否为空</span></span><br><span class="line">  </span><br><span class="line"><span class="type">int</span>   <span class="title function_">size</span><span class="params">()</span>	<span class="comment">//集合的长度，也就是集合中元素的个数</span></span><br></pre></td></tr></table></div></figure>




        <h2 id="1-4-Collection集合的遍历"   >
          <a href="#1-4-Collection集合的遍历" class="heading-link"><i class="fas fa-link"></i></a><a href="#1-4-Collection集合的遍历" class="headerlink" title="1.4 Collection集合的遍历"></a>1.4 Collection集合的遍历</h2>
      
        <h3 id="1-4-1-迭代器"   >
          <a href="#1-4-1-迭代器" class="heading-link"><i class="fas fa-link"></i></a><a href="#1-4-1-迭代器" class="headerlink" title="1.4.1 迭代器"></a>1.4.1 迭代器</h3>
      
        <h4 id="1-4-1-1-迭代器介绍"   >
          <a href="#1-4-1-1-迭代器介绍" class="heading-link"><i class="fas fa-link"></i></a><a href="#1-4-1-1-迭代器介绍" class="headerlink" title="1.4.1.1 迭代器介绍"></a>1.4.1.1 迭代器介绍</h4>
      <ul>
<li>迭代器,集合的专用遍历方式</li>
<li><code>Iterator&lt;E&gt; iterator()</code>: 返回此集合中元素的迭代器,通过集合对象的<code>iterator()方法</code>得到</li>
</ul>

        <h4 id="1-4-1-2-Iterator中的常用方法"   >
          <a href="#1-4-1-2-Iterator中的常用方法" class="heading-link"><i class="fas fa-link"></i></a><a href="#1-4-1-2-Iterator中的常用方法" class="headerlink" title="1.4.1.2 Iterator中的常用方法"></a>1.4.1.2 Iterator中的常用方法</h4>
      <figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">boolean</span> <span class="title function_">hasNext</span><span class="params">()</span>	<span class="comment">//判断当前位置是否有元素可以被取出</span></span><br><span class="line">E <span class="title function_">next</span><span class="params">()</span>	<span class="comment">//获取当前位置的元素,将迭代器对象移向下一个索引位置</span></span><br></pre></td></tr></table></div></figure>


        <h4 id="1-4-1-3-Collection集合的遍历"   >
          <a href="#1-4-1-3-Collection集合的遍历" class="heading-link"><i class="fas fa-link"></i></a><a href="#1-4-1-3-Collection集合的遍历" class="headerlink" title="1.4.1.3 Collection集合的遍历"></a>1.4.1.3 Collection集合的遍历</h4>
      <figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">IteratorDemo1</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">//创建集合对象</span></span><br><span class="line">        Collection&lt;String&gt; c = <span class="keyword">new</span> <span class="title class_">ArrayList</span>&lt;&gt;();</span><br><span class="line"></span><br><span class="line">        <span class="comment">//添加元素</span></span><br><span class="line">        c.add(<span class="string">&quot;hello&quot;</span>);</span><br><span class="line">        c.add(<span class="string">&quot;world&quot;</span>);</span><br><span class="line">        c.add(<span class="string">&quot;java&quot;</span>);</span><br><span class="line">        c.add(<span class="string">&quot;javaee&quot;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//Iterator&lt;E&gt; iterator()：返回此集合中元素的迭代器，通过集合的iterator()方法得到</span></span><br><span class="line">        Iterator&lt;String&gt; it = c.iterator();</span><br><span class="line"></span><br><span class="line">        <span class="comment">//用while循环改进元素的判断和获取</span></span><br><span class="line">        <span class="keyword">while</span> (it.hasNext()) &#123;</span><br><span class="line">            <span class="type">String</span> <span class="variable">s</span> <span class="operator">=</span> it.next();</span><br><span class="line">            System.out.println(s);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h4 id="1-4-1-4-迭代器中删除的方法"   >
          <a href="#1-4-1-4-迭代器中删除的方法" class="heading-link"><i class="fas fa-link"></i></a><a href="#1-4-1-4-迭代器中删除的方法" class="headerlink" title="1.4.1.4 迭代器中删除的方法"></a>1.4.1.4 迭代器中删除的方法</h4>
      <figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">void</span> <span class="title function_">remove</span><span class="params">()</span>	<span class="comment">//删除迭代器对象当前指向的元素</span></span><br></pre></td></tr></table></div></figure>

<p>​	示例：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">IteratorDemo2</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        ArrayList&lt;String&gt; list = <span class="keyword">new</span> <span class="title class_">ArrayList</span>&lt;&gt;();</span><br><span class="line">        list.add(<span class="string">&quot;a&quot;</span>);</span><br><span class="line">        list.add(<span class="string">&quot;b&quot;</span>);</span><br><span class="line">        list.add(<span class="string">&quot;b&quot;</span>);</span><br><span class="line">        list.add(<span class="string">&quot;c&quot;</span>);</span><br><span class="line">        list.add(<span class="string">&quot;d&quot;</span>);</span><br><span class="line"></span><br><span class="line">        Iterator&lt;String&gt; it = list.iterator();</span><br><span class="line">        <span class="keyword">while</span>(it.hasNext())&#123;</span><br><span class="line">            <span class="type">String</span> <span class="variable">s</span> <span class="operator">=</span> it.next();</span><br><span class="line">            <span class="keyword">if</span>(<span class="string">&quot;b&quot;</span>.equals(s))&#123;</span><br><span class="line">                <span class="comment">//指向谁,那么此时就删除谁.</span></span><br><span class="line">                it.remove();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        System.out.println(list);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h3 id="1-4-2-增强for循环"   >
          <a href="#1-4-2-增强for循环" class="heading-link"><i class="fas fa-link"></i></a><a href="#1-4-2-增强for循环" class="headerlink" title="1.4.2 增强for循环"></a>1.4.2 增强for循环</h3>
      <p>介绍：</p>
<ul>
<li>它是JDK5之后出现的,其内部原理是一个Iterator迭代器</li>
<li>实现Iterable接口的类才可以使用迭代器和增强for</li>
<li>简化数组和Collection集合的遍历</li>
</ul>
<p>格式：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span>(集合/数组中元素的数据类型 变量名 :  集合/数组名) &#123;</span><br><span class="line"></span><br><span class="line">		<span class="comment">// 已经将当前遍历到的元素封装到变量中了,直接使用变量即可</span></span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

<p>代码：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">MyCollectonDemo1</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        ArrayList&lt;String&gt; list =  <span class="keyword">new</span> <span class="title class_">ArrayList</span>&lt;&gt;();</span><br><span class="line">        list.add(<span class="string">&quot;a&quot;</span>);</span><br><span class="line">        list.add(<span class="string">&quot;b&quot;</span>);</span><br><span class="line">        list.add(<span class="string">&quot;c&quot;</span>);</span><br><span class="line">        list.add(<span class="string">&quot;d&quot;</span>);</span><br><span class="line">        list.add(<span class="string">&quot;e&quot;</span>);</span><br><span class="line">        list.add(<span class="string">&quot;f&quot;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//1,数据类型一定是集合或者数组中元素的类型</span></span><br><span class="line">        <span class="comment">//2,str仅仅是一个变量名而已,在循环的过程中,依次表示集合或者数组中的每一个元素</span></span><br><span class="line">        <span class="comment">//3,list就是要遍历的集合或者数组</span></span><br><span class="line">        <span class="keyword">for</span>(String str : list)&#123;</span><br><span class="line">            System.out.println(str);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h1 id="2-泛型"   >
          <a href="#2-泛型" class="heading-link"><i class="fas fa-link"></i></a><a href="#2-泛型" class="headerlink" title="2.泛型"></a>2.泛型</h1>
      
        <h2 id="2-1-泛型概述"   >
          <a href="#2-1-泛型概述" class="heading-link"><i class="fas fa-link"></i></a><a href="#2-1-泛型概述" class="headerlink" title="2.1 泛型概述"></a>2.1 泛型概述</h2>
      <p>泛型的介绍：泛型是JDK5中引入的特性，它提供了编译时类型安全检测机制</p>
<p>泛型的好处：</p>
<ol>
<li>把运行时期的问题提前到了编译期间</li>
<li>避免了强制类型转换</li>
</ol>
<p>泛型的定义格式：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">&lt;类型&gt;: 指定一种类型的格式.尖括号里面可以任意书写,一般只写一个字母.例如: &lt;E&gt; &lt;T&gt;</span><br><span class="line">&lt;类型<span class="number">1</span>,类型<span class="number">2</span>…&gt;: 指定多种类型的格式,多种类型之间用逗号隔开.例如: &lt;E,T&gt; &lt;K,V&gt; </span><br></pre></td></tr></table></div></figure>


        <h2 id="2-2-泛型类"   >
          <a href="#2-2-泛型类" class="heading-link"><i class="fas fa-link"></i></a><a href="#2-2-泛型类" class="headerlink" title="2.2 泛型类"></a>2.2 泛型类</h2>
      <p>定义格式：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">修饰符 class 类名&lt;类型&gt; &#123;  &#125;</span><br></pre></td></tr></table></div></figure>

<p>示例代码：</p>
<ul>
<li><p>泛型类</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Generic</span>&lt;T&gt; &#123;</span><br><span class="line">    <span class="keyword">private</span> T t;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> T <span class="title function_">getT</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> t;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">setT</span><span class="params">(T t)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.t = t;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>
</li>
<li><p>测试类</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">GenericDemo1</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        Generic&lt;String&gt; g1 = <span class="keyword">new</span> <span class="title class_">Generic</span>&lt;String&gt;();</span><br><span class="line">        g1.setT(<span class="string">&quot;杨幂&quot;</span>);</span><br><span class="line">        System.out.println(g1.getT());</span><br><span class="line"></span><br><span class="line">        Generic&lt;Integer&gt; g2 = <span class="keyword">new</span> <span class="title class_">Generic</span>&lt;Integer&gt;();</span><br><span class="line">        g2.setT(<span class="number">30</span>);</span><br><span class="line">        System.out.println(g2.getT());</span><br><span class="line"></span><br><span class="line">        Generic&lt;Boolean&gt; g3 = <span class="keyword">new</span> <span class="title class_">Generic</span>&lt;Boolean&gt;();</span><br><span class="line">        g3.setT(<span class="literal">true</span>);</span><br><span class="line">        System.out.println(g3.getT());</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure></li>
</ul>

        <h2 id="2-3-泛型方法"   >
          <a href="#2-3-泛型方法" class="heading-link"><i class="fas fa-link"></i></a><a href="#2-3-泛型方法" class="headerlink" title="2.3 泛型方法"></a>2.3 泛型方法</h2>
      <p>定义格式：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">修饰符 &lt;类型&gt; 返回值类型 方法名(类型 变量名) &#123;  &#125;</span><br></pre></td></tr></table></div></figure>

<p>示例代码：</p>
<ul>
<li><p>带有泛型方法的类</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Generic</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> &lt;T&gt; <span class="keyword">void</span> <span class="title function_">show</span><span class="params">(T t)</span> &#123;</span><br><span class="line">        System.out.println(t);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>
</li>
<li><p>测试类</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">GenericDemo2</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">	    <span class="type">Generic</span> <span class="variable">g</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Generic</span>();</span><br><span class="line">        g.show(<span class="string">&quot;柳岩&quot;</span>);</span><br><span class="line">        g.show(<span class="number">30</span>);</span><br><span class="line">        g.show(<span class="literal">true</span>);</span><br><span class="line">        g.show(<span class="number">12.34</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure></li>
</ul>

        <h2 id="2-4-泛型接口"   >
          <a href="#2-4-泛型接口" class="heading-link"><i class="fas fa-link"></i></a><a href="#2-4-泛型接口" class="headerlink" title="2.4 泛型接口"></a>2.4 泛型接口</h2>
      <p>定义格式：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">修饰符 interface 接口名&lt;类型&gt; &#123;  &#125;</span><br></pre></td></tr></table></div></figure>

<p>示例代码：</p>
<ul>
<li><p>泛型接口</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">interface</span> <span class="title class_">Generic</span>&lt;T&gt; &#123;</span><br><span class="line">    <span class="keyword">void</span> <span class="title function_">show</span><span class="params">(T t)</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>
</li>
<li><p>泛型接口实现类1</p>
<p>​	定义实现类时,定义和接口相同泛型,创建实现类对象时明确泛型的具体类型</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">GenericImpl1</span>&lt;T&gt; <span class="keyword">implements</span> <span class="title class_">Generic</span>&lt;T&gt; &#123;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">show</span><span class="params">(T t)</span> &#123;</span><br><span class="line">        System.out.println(t);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>
</li>
<li><p>泛型接口实现类2</p>
<p>​	定义实现类时,直接明确泛型的具体类型</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">GenericImpl2</span> <span class="keyword">implements</span> <span class="title class_">Generic</span>&lt;Integer&gt;&#123;</span><br><span class="line">     <span class="meta">@Override</span></span><br><span class="line">     <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">show</span><span class="params">(Integer t)</span> &#123;</span><br><span class="line">          System.out.println(t);</span><br><span class="line">     &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>
</li>
<li><p>测试类</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">GenericDemo3</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        GenericImpl1&lt;String&gt; g1 = <span class="keyword">new</span> <span class="title class_">GenericImpl</span>&lt;String&gt;();</span><br><span class="line">        g1.show(<span class="string">&quot;林青霞&quot;</span>);</span><br><span class="line">        GenericImpl1&lt;Integer&gt; g2 = <span class="keyword">new</span> <span class="title class_">GenericImpl</span>&lt;Integer&gt;();</span><br><span class="line">        g2.show(<span class="number">30</span>);</span><br><span class="line">      </span><br><span class="line">        <span class="type">GenericImpl2</span> <span class="variable">g3</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">GenericImpl2</span>();</span><br><span class="line">      	g3.show(<span class="number">10</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></div></figure></li>
</ul>

        <h2 id="2-5-类型通配符"   >
          <a href="#2-5-类型通配符" class="heading-link"><i class="fas fa-link"></i></a><a href="#2-5-类型通配符" class="headerlink" title="2.5 类型通配符"></a>2.5 类型通配符</h2>
      <ul>
<li><p>类型通配符: <code>&lt;?&gt;</code></p>
<ul>
<li><code>ArrayList&lt;?&gt;</code>: 表示元素类型未知的<code>ArrayList</code>,它的元素可以匹配任何的类型</li>
<li>但是并不能把元素添加到<code>ArrayList</code>中了,获取出来的也是父类类型</li>
</ul>
</li>
<li><p>类型通配符上限: <code>&lt;? extends 类型&gt;</code></p>
<ul>
<li><code>ArrayListList &lt;? extends Number&gt;</code>: 它表示的类型是Number或者其子类型</li>
</ul>
</li>
<li><p>类型通配符下限: <code>&lt;? super 类型&gt;</code></p>
<ul>
<li><code>ArrayListList &lt;? super Number&gt;</code>: 它表示的类型是Number或者其父类型</li>
</ul>
</li>
<li><p>泛型通配符的使用</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">GenericDemo4</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        ArrayList&lt;Integer&gt; list1 = <span class="keyword">new</span> <span class="title class_">ArrayList</span>&lt;&gt;();</span><br><span class="line">        ArrayList&lt;String&gt; list2 = <span class="keyword">new</span> <span class="title class_">ArrayList</span>&lt;&gt;();</span><br><span class="line">        ArrayList&lt;Number&gt; list3 = <span class="keyword">new</span> <span class="title class_">ArrayList</span>&lt;&gt;();</span><br><span class="line">        ArrayList&lt;Object&gt; list4 = <span class="keyword">new</span> <span class="title class_">ArrayList</span>&lt;&gt;();</span><br><span class="line"></span><br><span class="line">        method(list1);</span><br><span class="line">        method(list2);</span><br><span class="line">        method(list3);</span><br><span class="line">        method(list4);</span><br><span class="line"></span><br><span class="line">        getElement1(list1);</span><br><span class="line">        getElement1(list2);<span class="comment">//报错</span></span><br><span class="line">        getElement1(list3);</span><br><span class="line">        getElement1(list4);<span class="comment">//报错</span></span><br><span class="line"></span><br><span class="line">        getElement2(list1);<span class="comment">//报错</span></span><br><span class="line">        getElement2(list2);<span class="comment">//报错</span></span><br><span class="line">        getElement2(list3);</span><br><span class="line">        getElement2(list4);</span><br><span class="line">    &#125;</span><br><span class="line">  </span><br><span class="line">    <span class="comment">// 泛型通配符: 此时的泛型?,可以是任意类型</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">method</span><span class="params">(ArrayList&lt;?&gt; list)</span>&#123;&#125;</span><br><span class="line">  </span><br><span class="line">    <span class="comment">// 泛型的上限: 此时的泛型?,必须是Number类型或者Number类型的子类</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">getElement1</span><span class="params">(ArrayList&lt;? extends Number&gt; list)</span>&#123;&#125;</span><br><span class="line">  </span><br><span class="line">    <span class="comment">// 泛型的下限: 此时的泛型?,必须是Number类型或者Number类型的父类</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">getElement2</span><span class="params">(ArrayList&lt;? <span class="built_in">super</span> Number&gt; list)</span>&#123;&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure></li>
</ul>

        <h1 id="3-List集合"   >
          <a href="#3-List集合" class="heading-link"><i class="fas fa-link"></i></a><a href="#3-List集合" class="headerlink" title="3.List集合"></a>3.List集合</h1>
      
        <h2 id="3-1-概述和特点"   >
          <a href="#3-1-概述和特点" class="heading-link"><i class="fas fa-link"></i></a><a href="#3-1-概述和特点" class="headerlink" title="3.1 概述和特点"></a>3.1 概述和特点</h2>
      <p>概述：</p>
<ul>
<li>有序集合,这里的有序指的是存取顺序</li>
<li>用户可以精确控制列表中每个元素的插入位置,用户可以通过整数索引访问元素,并搜索列表中的元素</li>
<li>与Set集合不同,列表通常允许重复的元素</li>
</ul>
<p>特点：</p>
<ul>
<li>存取有序</li>
<li>可以重复</li>
<li>有索引</li>
</ul>

        <h2 id="3-2-List集合的特有方法"   >
          <a href="#3-2-List集合的特有方法" class="heading-link"><i class="fas fa-link"></i></a><a href="#3-2-List集合的特有方法" class="headerlink" title="3.2 List集合的特有方法"></a>3.2 List集合的特有方法</h2>
      <figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">void</span> <span class="title function_">add</span><span class="params">(<span class="type">int</span> index,E element)</span>	<span class="comment">//在此集合中的指定位置插入指定的元素</span></span><br><span class="line">  </span><br><span class="line">E <span class="title function_">remove</span><span class="params">(<span class="type">int</span> index)</span>	<span class="comment">//删除指定索引处的元素，返回被删除的元素</span></span><br><span class="line">  </span><br><span class="line">E <span class="title function_">set</span><span class="params">(<span class="type">int</span> index,E element)</span>	<span class="comment">//修改指定索引处的元素，返回被修改的元素</span></span><br><span class="line">  </span><br><span class="line">E <span class="title function_">get</span><span class="params">(<span class="type">int</span> index)</span>	<span class="comment">//返回指定索引处的元素</span></span><br></pre></td></tr></table></div></figure>




        <h2 id="3-1-List集合的实现类"   >
          <a href="#3-1-List集合的实现类" class="heading-link"><i class="fas fa-link"></i></a><a href="#3-1-List集合的实现类" class="headerlink" title="3.1 List集合的实现类"></a>3.1 List集合的实现类</h2>
      
        <h3 id="3-1-1-List集合子类的特点"   >
          <a href="#3-1-1-List集合子类的特点" class="heading-link"><i class="fas fa-link"></i></a><a href="#3-1-1-List集合子类的特点" class="headerlink" title="3.1.1 List集合子类的特点"></a>3.1.1 List集合子类的特点</h3>
      <ul>
<li><p>ArrayList集合</p>
<p>​	底层是数组结构实现，查询快、增删慢</p>
</li>
<li><p>LinkedList集合</p>
<p>​	底层是链表结构实现，查询慢、增删快</p>
</li>
</ul>

        <h3 id="3-1-2-ArrayList"   >
          <a href="#3-1-2-ArrayList" class="heading-link"><i class="fas fa-link"></i></a><a href="#3-1-2-ArrayList" class="headerlink" title="3.1.2 ArrayList"></a>3.1.2 ArrayList</h3>
      
        <h4 id="3-1-2-1-构造方法和添加方法"   >
          <a href="#3-1-2-1-构造方法和添加方法" class="heading-link"><i class="fas fa-link"></i></a><a href="#3-1-2-1-构造方法和添加方法" class="headerlink" title="3.1.2.1 构造方法和添加方法"></a>3.1.2.1 构造方法和添加方法</h4>
      <figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="title function_">ArrayList</span><span class="params">()</span>	<span class="comment">//创建一个空的集合对象</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> <span class="type">boolean</span> <span class="title function_">add</span><span class="params">(E e)</span>		<span class="comment">//将指定的元素追加到此集合的末尾</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">add</span><span class="params">(<span class="type">int</span> index,E element)</span>	<span class="comment">//在此集合中的指定位置插入指定的元素</span></span><br></pre></td></tr></table></div></figure>

<p><code>ArrayList&lt;E&gt;</code>  ：</p>
<p>​	可调整大小的数组实现 </p>
<p>​	<code>&lt;E&gt;</code> : 是一种特殊的数据类型，泛型。</p>
<p>怎么用呢 ?</p>
<p>​	在出现E的地方我们使用引用数据类型替换即可	</p>
<p>​	举例：<code>ArrayList&lt;String&gt;</code>, <code>ArrayList&lt;Student&gt;</code></p>

        <h4 id="3-1-2-2-常用方法"   >
          <a href="#3-1-2-2-常用方法" class="heading-link"><i class="fas fa-link"></i></a><a href="#3-1-2-2-常用方法" class="headerlink" title="3.1.2.2 常用方法"></a>3.1.2.2 常用方法</h4>
      <figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="type">boolean</span> <span class="title function_">remove</span><span class="params">(Object o)</span>	<span class="comment">//删除指定的元素，返回删除是否成功</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> E <span class="title function_">remove</span><span class="params">(<span class="type">int</span> index)</span>	<span class="comment">//删除指定索引处的元素，返回被删除的元素</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> E <span class="title function_">set</span><span class="params">(<span class="type">int</span> index,E element)</span>	<span class="comment">//修改指定索引处的元素，返回被修改的元素</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> E <span class="title function_">get</span><span class="params">(<span class="type">int</span> index)</span>	<span class="comment">//返回指定索引处的元素</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> <span class="type">int</span> <span class="title function_">size</span><span class="params">()</span>	<span class="comment">//返回集合中的元素的个数</span></span><br></pre></td></tr></table></div></figure>


        <h3 id="3-1-3-LinkedList"   >
          <a href="#3-1-3-LinkedList" class="heading-link"><i class="fas fa-link"></i></a><a href="#3-1-3-LinkedList" class="headerlink" title="3.1.3 LinkedList"></a>3.1.3 LinkedList</h3>
      <p>特有方法：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">addFirst</span><span class="params">(E e)</span>	<span class="comment">//在该列表开头插入指定的元素</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">addLast</span><span class="params">(E e)</span>	<span class="comment">//将指定的元素追加到此列表的末尾</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> E <span class="title function_">getFirst</span><span class="params">()</span>	<span class="comment">//返回此列表中的第一个元素</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> E <span class="title function_">getLast</span><span class="params">()</span>	<span class="comment">//返回此列表中的最后一个元素</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> E <span class="title function_">removeFirst</span><span class="params">()</span>	<span class="comment">//从此列表中删除并返回第一个元素</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> E <span class="title function_">removeLast</span><span class="params">()</span>	<span class="comment">//从此列表中删除并返回最后一个元素</span></span><br></pre></td></tr></table></div></figure>




        <h1 id="4-Set集合"   >
          <a href="#4-Set集合" class="heading-link"><i class="fas fa-link"></i></a><a href="#4-Set集合" class="headerlink" title="4.Set集合"></a>4.Set集合</h1>
      
        <h2 id="4-1-概述和特点"   >
          <a href="#4-1-概述和特点" class="heading-link"><i class="fas fa-link"></i></a><a href="#4-1-概述和特点" class="headerlink" title="4.1 概述和特点"></a>4.1 概述和特点</h2>
      <ul>
<li>不可以存储重复元素</li>
<li>没有索引,不能使用普通for循环遍历</li>
</ul>

        <h2 id="4-2-Set集合的使用"   >
          <a href="#4-2-Set集合的使用" class="heading-link"><i class="fas fa-link"></i></a><a href="#4-2-Set集合的使用" class="headerlink" title="4.2 Set集合的使用"></a>4.2 Set集合的使用</h2>
      <p>存储字符串并遍历</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">MySet1</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">      	<span class="comment">//创建集合对象</span></span><br><span class="line">        Set&lt;String&gt; set = <span class="keyword">new</span> <span class="title class_">TreeSet</span>&lt;&gt;();</span><br><span class="line">      	<span class="comment">//添加元素</span></span><br><span class="line">        set.add(<span class="string">&quot;ccc&quot;</span>);</span><br><span class="line">        set.add(<span class="string">&quot;aaa&quot;</span>);</span><br><span class="line">        set.add(<span class="string">&quot;aaa&quot;</span>);</span><br><span class="line">        set.add(<span class="string">&quot;bbb&quot;</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">//        for (int i = 0; i &lt; set.size(); i++) &#123;</span></span><br><span class="line"><span class="comment">//            //Set集合是没有索引的，所以不能使用通过索引获取元素的方法</span></span><br><span class="line"><span class="comment">//        &#125;</span></span><br><span class="line">      </span><br><span class="line">      	<span class="comment">//遍历集合</span></span><br><span class="line">        Iterator&lt;String&gt; it = set.iterator();</span><br><span class="line">        <span class="keyword">while</span> (it.hasNext())&#123;</span><br><span class="line">            <span class="type">String</span> <span class="variable">s</span> <span class="operator">=</span> it.next();</span><br><span class="line">            System.out.println(s);</span><br><span class="line">        &#125;</span><br><span class="line">        System.out.println(<span class="string">&quot;-----------------------------------&quot;</span>);</span><br><span class="line">        <span class="keyword">for</span> (String s : set) &#123;</span><br><span class="line">            System.out.println(s);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h2 id="4-3-Set集合实现类"   >
          <a href="#4-3-Set集合实现类" class="heading-link"><i class="fas fa-link"></i></a><a href="#4-3-Set集合实现类" class="headerlink" title="4.3 Set集合实现类"></a>4.3 Set集合实现类</h2>
      
        <h3 id="4-3-1-TreeSet"   >
          <a href="#4-3-1-TreeSet" class="heading-link"><i class="fas fa-link"></i></a><a href="#4-3-1-TreeSet" class="headerlink" title="4.3.1 TreeSet"></a>4.3.1 TreeSet</h3>
      
        <h4 id="4-3-1-1概述和特点"   >
          <a href="#4-3-1-1概述和特点" class="heading-link"><i class="fas fa-link"></i></a><a href="#4-3-1-1概述和特点" class="headerlink" title="4.3.1.1概述和特点"></a>4.3.1.1概述和特点</h4>
      <ul>
<li>不可以存储重复元素</li>
<li>没有索引</li>
<li>可以将元素按照规则进行排序<ul>
<li><code>TreeSet()</code>：根据其元素的自然排序进行排序</li>
<li><code>TreeSet(Comparator comparator)</code> ：根据指定的比较器进行排序</li>
</ul>
</li>
</ul>

        <h4 id="4-3-1-2-集合基本使用"   >
          <a href="#4-3-1-2-集合基本使用" class="heading-link"><i class="fas fa-link"></i></a><a href="#4-3-1-2-集合基本使用" class="headerlink" title="4.3.1.2 集合基本使用"></a>4.3.1.2 集合基本使用</h4>
      <p>存储Integer类型的整数并遍历</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">TreeSetDemo01</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">//创建集合对象</span></span><br><span class="line">        TreeSet&lt;Integer&gt; ts = <span class="keyword">new</span> <span class="title class_">TreeSet</span>&lt;Integer&gt;();</span><br><span class="line"></span><br><span class="line">        <span class="comment">//添加元素</span></span><br><span class="line">        ts.add(<span class="number">10</span>);</span><br><span class="line">        ts.add(<span class="number">40</span>);</span><br><span class="line">        ts.add(<span class="number">30</span>);</span><br><span class="line">        ts.add(<span class="number">50</span>);</span><br><span class="line">        ts.add(<span class="number">20</span>);</span><br><span class="line"></span><br><span class="line">        ts.add(<span class="number">30</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//遍历集合</span></span><br><span class="line">        <span class="keyword">for</span>(Integer i : ts) &#123;</span><br><span class="line">            System.out.println(i);</span><br><span class="line">        &#125;</span><br><span class="line">      <span class="comment">//10	20	30	40	50</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h4 id="4-3-1-3-自然排序Comparable的使用"   >
          <a href="#4-3-1-3-自然排序Comparable的使用" class="heading-link"><i class="fas fa-link"></i></a><a href="#4-3-1-3-自然排序Comparable的使用" class="headerlink" title="4.3.1.3 自然排序Comparable的使用"></a>4.3.1.3 自然排序Comparable的使用</h4>
      <p>案例需求:</p>
<ol>
<li>存储学生对象并遍历，创建TreeSet集合使用无参构造方法</li>
<li>要求：按照年龄从小到大排序，年龄相同时，按照姓名的字母顺序排序</li>
</ol>
<p>实现步骤:</p>
<ol>
<li>使用空参构造创建TreeSet集合<ul>
<li>用TreeSet集合存储自定义对象，无参构造方法使用的是自然排序对元素进行排序的</li>
</ul>
</li>
<li>自定义的Student类实现Comparable接口<ul>
<li>自然排序，就是让元素所属的类实现Comparable接口，重写compareTo(T o)方法</li>
</ul>
</li>
<li>重写接口中的compareTo方法<ul>
<li>重写方法时，一定要注意排序规则必须按照要求的主要条件和次要条件来写</li>
</ul>
</li>
</ol>
<p>代码实现:</p>
<p>学生类</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Student</span> <span class="keyword">implements</span> <span class="title class_">Comparable</span>&lt;Student&gt;&#123;</span><br><span class="line">    <span class="keyword">private</span> String name;</span><br><span class="line">    <span class="keyword">private</span> <span class="type">int</span> age;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="title function_">Student</span><span class="params">()</span> &#123;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="title function_">Student</span><span class="params">(String name, <span class="type">int</span> age)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.name = name;</span><br><span class="line">        <span class="built_in">this</span>.age = age;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> String <span class="title function_">getName</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">setName</span><span class="params">(String name)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.name = name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="type">int</span> <span class="title function_">getAge</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> age;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">setAge</span><span class="params">(<span class="type">int</span> age)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.age = age;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> String <span class="title function_">toString</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">&quot;Student&#123;&quot;</span> +</span><br><span class="line">                <span class="string">&quot;name=&#x27;&quot;</span> + name + <span class="string">&#x27;\&#x27;&#x27;</span> +</span><br><span class="line">                <span class="string">&quot;, age=&quot;</span> + age +</span><br><span class="line">                <span class="string">&#x27;&#125;&#x27;</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> <span class="type">int</span> <span class="title function_">compareTo</span><span class="params">(Student o)</span> &#123;</span><br><span class="line">        <span class="comment">//按照对象的年龄进行排序</span></span><br><span class="line">        <span class="comment">//主要判断条件: 按照年龄从小到大排序</span></span><br><span class="line">        <span class="type">int</span> <span class="variable">result</span> <span class="operator">=</span> <span class="built_in">this</span>.age - o.age;</span><br><span class="line">        <span class="comment">//次要判断条件: 年龄相同时，按照姓名的字母顺序排序</span></span><br><span class="line">        result = result == <span class="number">0</span> ? <span class="built_in">this</span>.name.compareTo(o.getName()) : result;</span><br><span class="line">        <span class="keyword">return</span> result;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

<p>测试类</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">MyTreeSet2</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">//创建集合对象</span></span><br><span class="line">        TreeSet&lt;Student&gt; ts = <span class="keyword">new</span> <span class="title class_">TreeSet</span>&lt;&gt;();</span><br><span class="line">	    <span class="comment">//创建学生对象</span></span><br><span class="line">        <span class="type">Student</span> <span class="variable">s1</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Student</span>(<span class="string">&quot;zhangsan&quot;</span>,<span class="number">28</span>);</span><br><span class="line">        <span class="type">Student</span> <span class="variable">s2</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Student</span>(<span class="string">&quot;lisi&quot;</span>,<span class="number">27</span>);</span><br><span class="line">        <span class="type">Student</span> <span class="variable">s3</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Student</span>(<span class="string">&quot;wangwu&quot;</span>,<span class="number">29</span>);</span><br><span class="line">        <span class="type">Student</span> <span class="variable">s4</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Student</span>(<span class="string">&quot;zhaoliu&quot;</span>,<span class="number">28</span>);</span><br><span class="line">        <span class="type">Student</span> <span class="variable">s5</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Student</span>(<span class="string">&quot;qianqi&quot;</span>,<span class="number">30</span>);</span><br><span class="line">		<span class="comment">//把学生添加到集合</span></span><br><span class="line">        ts.add(s1);</span><br><span class="line">        ts.add(s2);</span><br><span class="line">        ts.add(s3);</span><br><span class="line">        ts.add(s4);</span><br><span class="line">        ts.add(s5);</span><br><span class="line">		<span class="comment">//遍历集合</span></span><br><span class="line">        <span class="keyword">for</span> (Student student : ts) &#123;</span><br><span class="line">            System.out.println(student);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h4 id="4-3-1-4-比较器排序Comparator的使用"   >
          <a href="#4-3-1-4-比较器排序Comparator的使用" class="heading-link"><i class="fas fa-link"></i></a><a href="#4-3-1-4-比较器排序Comparator的使用" class="headerlink" title="4.3.1.4 比较器排序Comparator的使用"></a>4.3.1.4 比较器排序Comparator的使用</h4>
      <p>案例需求:</p>
<ol>
<li>存储老师对象并遍历，创建TreeSet集合使用带参构造方法</li>
<li>要求：按照年龄从小到大排序，年龄相同时，按照姓名的字母顺序排序</li>
</ol>
<p>实现步骤:</p>
<ol>
<li>用TreeSet集合存储自定义对象，带参构造方法使用的是比较器排序对元素进行排序的</li>
<li>比较器排序，就是让集合构造方法接收Comparator的实现类对象，重写compare(T o1,T o2)方法</li>
<li>重写方法时，一定要注意排序规则必须按照要求的主要条件和次要条件来写</li>
</ol>
<p>代码实现:</p>
<p>老师类</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Teacher</span> &#123;</span><br><span class="line">    <span class="keyword">private</span> String name;</span><br><span class="line">    <span class="keyword">private</span> <span class="type">int</span> age;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="title function_">Teacher</span><span class="params">()</span> &#123;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="title function_">Teacher</span><span class="params">(String name, <span class="type">int</span> age)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.name = name;</span><br><span class="line">        <span class="built_in">this</span>.age = age;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> String <span class="title function_">getName</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">setName</span><span class="params">(String name)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.name = name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="type">int</span> <span class="title function_">getAge</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> age;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">setAge</span><span class="params">(<span class="type">int</span> age)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.age = age;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> String <span class="title function_">toString</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">&quot;Teacher&#123;&quot;</span> +</span><br><span class="line">                <span class="string">&quot;name=&#x27;&quot;</span> + name + <span class="string">&#x27;\&#x27;&#x27;</span> +</span><br><span class="line">                <span class="string">&quot;, age=&quot;</span> + age +</span><br><span class="line">                <span class="string">&#x27;&#125;&#x27;</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

<p>测试类</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">MyTreeSet4</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">      	<span class="comment">//创建集合对象</span></span><br><span class="line">        TreeSet&lt;Teacher&gt; ts = <span class="keyword">new</span> <span class="title class_">TreeSet</span>&lt;&gt;(<span class="keyword">new</span> <span class="title class_">Comparator</span>&lt;Teacher&gt;() &#123;</span><br><span class="line">            <span class="meta">@Override</span></span><br><span class="line">            <span class="keyword">public</span> <span class="type">int</span> <span class="title function_">compare</span><span class="params">(Teacher o1, Teacher o2)</span> &#123;</span><br><span class="line">                <span class="comment">//o1表示现在要存入的那个元素</span></span><br><span class="line">                <span class="comment">//o2表示已经存入到集合中的元素</span></span><br><span class="line">              </span><br><span class="line">                <span class="comment">//主要条件</span></span><br><span class="line">                <span class="type">int</span> <span class="variable">result</span> <span class="operator">=</span> o1.getAge() - o2.getAge();</span><br><span class="line">                <span class="comment">//次要条件</span></span><br><span class="line">                result = result == <span class="number">0</span> ? o1.getName().compareTo(o2.getName()) : result;</span><br><span class="line">                <span class="keyword">return</span> result;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;);</span><br><span class="line">		<span class="comment">//创建老师对象</span></span><br><span class="line">        <span class="type">Teacher</span> <span class="variable">t1</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Teacher</span>(<span class="string">&quot;zhangsan&quot;</span>,<span class="number">23</span>);</span><br><span class="line">        <span class="type">Teacher</span> <span class="variable">t2</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Teacher</span>(<span class="string">&quot;lisi&quot;</span>,<span class="number">22</span>);</span><br><span class="line">        <span class="type">Teacher</span> <span class="variable">t3</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Teacher</span>(<span class="string">&quot;wangwu&quot;</span>,<span class="number">24</span>);</span><br><span class="line">        <span class="type">Teacher</span> <span class="variable">t4</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Teacher</span>(<span class="string">&quot;zhaoliu&quot;</span>,<span class="number">24</span>);</span><br><span class="line">		<span class="comment">//把老师添加到集合</span></span><br><span class="line">        ts.add(t1);</span><br><span class="line">        ts.add(t2);</span><br><span class="line">        ts.add(t3);</span><br><span class="line">        ts.add(t4);</span><br><span class="line">		<span class="comment">//遍历集合</span></span><br><span class="line">        <span class="keyword">for</span> (Teacher teacher : ts) &#123;</span><br><span class="line">            System.out.println(teacher);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h4 id="4-3-1-5-两种比较方式总结"   >
          <a href="#4-3-1-5-两种比较方式总结" class="heading-link"><i class="fas fa-link"></i></a><a href="#4-3-1-5-两种比较方式总结" class="headerlink" title="4.3.1.5 两种比较方式总结"></a>4.3.1.5 两种比较方式总结</h4>
      <ul>
<li>两种比较方式小结<ul>
<li>自然排序: 自定义类实现Comparable接口,重写compareTo方法,根据返回值进行排序</li>
<li>比较器排序: 创建TreeSet对象的时候传递Comparator的实现类对象,重写compare方法,根据返回值进行排序</li>
<li>在使用的时候,默认使用自然排序,当自然排序不满足现在的需求时,必须使用比较器排序</li>
</ul>
</li>
<li>两种方式中关于返回值的规则<ul>
<li>如果返回值为负数，表示当前存入的元素是较小值，存左边</li>
<li>如果返回值为0，表示当前存入的元素跟集合中元素重复了，不存</li>
<li>如果返回值为正数，表示当前存入的元素是较大值，存右边</li>
</ul>
</li>
</ul>

        <h3 id="4-3-2-HashSet"   >
          <a href="#4-3-2-HashSet" class="heading-link"><i class="fas fa-link"></i></a><a href="#4-3-2-HashSet" class="headerlink" title="4.3.2 HashSet"></a>4.3.2 HashSet</h3>
      
        <h4 id="4-3-2-1-概述和特点"   >
          <a href="#4-3-2-1-概述和特点" class="heading-link"><i class="fas fa-link"></i></a><a href="#4-3-2-1-概述和特点" class="headerlink" title="4.3.2.1 概述和特点"></a>4.3.2.1 概述和特点</h4>
      <ul>
<li>底层数据结构是哈希表</li>
<li>存取无序</li>
<li>不可以存储重复元素</li>
<li>没有索引,不能使用普通for循环遍历</li>
</ul>

        <h4 id="4-3-2-2-基本应用"   >
          <a href="#4-3-2-2-基本应用" class="heading-link"><i class="fas fa-link"></i></a><a href="#4-3-2-2-基本应用" class="headerlink" title="4.3.2.2 基本应用"></a>4.3.2.2 基本应用</h4>
      <p>存储字符串并遍历</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">HashSetDemo</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">//创建集合对象</span></span><br><span class="line">        HashSet&lt;String&gt; set = <span class="keyword">new</span> <span class="title class_">HashSet</span>&lt;String&gt;();</span><br><span class="line"></span><br><span class="line">        <span class="comment">//添加元素</span></span><br><span class="line">        set.add(<span class="string">&quot;hello&quot;</span>);</span><br><span class="line">        set.add(<span class="string">&quot;world&quot;</span>);</span><br><span class="line">        set.add(<span class="string">&quot;java&quot;</span>);</span><br><span class="line">        <span class="comment">//不包含重复元素的集合</span></span><br><span class="line">        set.add(<span class="string">&quot;world&quot;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//遍历</span></span><br><span class="line">        <span class="keyword">for</span>(String s : set) &#123;</span><br><span class="line">            System.out.println(s);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h4 id="4-3-2-3-哈希值"   >
          <a href="#4-3-2-3-哈希值" class="heading-link"><i class="fas fa-link"></i></a><a href="#4-3-2-3-哈希值" class="headerlink" title="4.3.2.3 哈希值"></a>4.3.2.3 哈希值</h4>
      <p>哈希值简介：</p>
<p>​	是JDK根据对象的地址或者字符串或者数字算出来的int类型的数值</p>
<p>如何获取哈希值：</p>
<p>​	<code>Object</code>类中的<code>public int hashCode()</code>：返回对象的哈希码值</p>
<p>哈希值的特点：</p>
<ul>
<li>同一个对象多次调用hashCode()方法返回的哈希值是相同的</li>
<li>默认情况下，不同对象的哈希值是不同的。而重写hashCode()方法，可以实现让不同对象的哈希值相同</li>
</ul>

        <h4 id="4-3-2-4-哈希表结构"   >
          <a href="#4-3-2-4-哈希表结构" class="heading-link"><i class="fas fa-link"></i></a><a href="#4-3-2-4-哈希表结构" class="headerlink" title="4.3.2.4 哈希表结构"></a>4.3.2.4 哈希表结构</h4>
      <p>JDK1.8以前</p>
<p>​	数组 + 链表</p>
<p><img src="https://s2.loli.net/2022/02/16/ohlkCrNfOveXpLD.png" alt="14_JKD8以前哈希表"> JDK1.8以后</p>
<ul>
<li><p>节点个数少于等于8个</p>
<p>​	数组 + 链表</p>
</li>
<li><p>节点个数多于8个</p>
<p>​	数组 + 红黑树</p>
</li>
</ul>
<p><img src="https://s2.loli.net/2022/02/16/ECLbPKZOpUGig8c.png" alt="15_JKD8以后哈希表"></p>

        <h4 id="4-3-2-5-HashSet集合存储学生对象并遍历"   >
          <a href="#4-3-2-5-HashSet集合存储学生对象并遍历" class="heading-link"><i class="fas fa-link"></i></a><a href="#4-3-2-5-HashSet集合存储学生对象并遍历" class="headerlink" title="4.3.2.5 HashSet集合存储学生对象并遍历"></a>4.3.2.5 HashSet集合存储学生对象并遍历</h4>
      <p>案例需求：</p>
<ul>
<li>创建一个存储学生对象的集合，存储多个学生对象，使用程序实现在控制台遍历该集合</li>
<li>要求：学生对象的成员变量值相同，我们就认为是同一个对象</li>
</ul>
<p>代码实现：</p>
<p>学生类</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Student</span> &#123;</span><br><span class="line">    <span class="keyword">private</span> String name;</span><br><span class="line">    <span class="keyword">private</span> <span class="type">int</span> age;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="title function_">Student</span><span class="params">()</span> &#123;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="title function_">Student</span><span class="params">(String name, <span class="type">int</span> age)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.name = name;</span><br><span class="line">        <span class="built_in">this</span>.age = age;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> String <span class="title function_">getName</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">setName</span><span class="params">(String name)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.name = name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="type">int</span> <span class="title function_">getAge</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> age;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">setAge</span><span class="params">(<span class="type">int</span> age)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.age = age;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> <span class="type">boolean</span> <span class="title function_">equals</span><span class="params">(Object o)</span> &#123;</span><br><span class="line">        <span class="keyword">if</span> (<span class="built_in">this</span> == o) <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">        <span class="keyword">if</span> (o == <span class="literal">null</span> || getClass() != o.getClass()) <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line"></span><br><span class="line">        <span class="type">Student</span> <span class="variable">student</span> <span class="operator">=</span> (Student) o;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (age != student.age) <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">        <span class="keyword">return</span> name != <span class="literal">null</span> ? name.equals(student.name) : student.name == <span class="literal">null</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> <span class="type">int</span> <span class="title function_">hashCode</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="type">int</span> <span class="variable">result</span> <span class="operator">=</span> name != <span class="literal">null</span> ? name.hashCode() : <span class="number">0</span>;</span><br><span class="line">        result = <span class="number">31</span> * result + age;</span><br><span class="line">        <span class="keyword">return</span> result;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

<p>测试类</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">HashSetDemo02</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">//创建HashSet集合对象</span></span><br><span class="line">        HashSet&lt;Student&gt; hs = <span class="keyword">new</span> <span class="title class_">HashSet</span>&lt;Student&gt;();</span><br><span class="line"></span><br><span class="line">        <span class="comment">//创建学生对象</span></span><br><span class="line">        <span class="type">Student</span> <span class="variable">s1</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Student</span>(<span class="string">&quot;林青霞&quot;</span>, <span class="number">30</span>);</span><br><span class="line">        <span class="type">Student</span> <span class="variable">s2</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Student</span>(<span class="string">&quot;张曼玉&quot;</span>, <span class="number">35</span>);</span><br><span class="line">        <span class="type">Student</span> <span class="variable">s3</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Student</span>(<span class="string">&quot;王祖贤&quot;</span>, <span class="number">33</span>);</span><br><span class="line"></span><br><span class="line">        <span class="type">Student</span> <span class="variable">s4</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Student</span>(<span class="string">&quot;王祖贤&quot;</span>, <span class="number">33</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//把学生添加到集合</span></span><br><span class="line">        hs.add(s1);</span><br><span class="line">        hs.add(s2);</span><br><span class="line">        hs.add(s3);</span><br><span class="line">        hs.add(s4);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//遍历集合(增强for)</span></span><br><span class="line">        <span class="keyword">for</span> (Student s : hs) &#123;</span><br><span class="line">            System.out.println(s.getName() + <span class="string">&quot;,&quot;</span> + s.getAge());</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

<p>总结：	HashSet集合存储自定义类型元素,要想实现元素的唯一,要求必须重写hashCode方法和equals方法</p>

        <h1 id="5-Map集合"   >
          <a href="#5-Map集合" class="heading-link"><i class="fas fa-link"></i></a><a href="#5-Map集合" class="headerlink" title="5.Map集合"></a>5.Map集合</h1>
      
        <h2 id="5-1-概述和特点"   >
          <a href="#5-1-概述和特点" class="heading-link"><i class="fas fa-link"></i></a><a href="#5-1-概述和特点" class="headerlink" title="5.1 概述和特点"></a>5.1 概述和特点</h2>
      <p>Map集合概述</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">interface</span> <span class="title class_">Map</span>&lt;K,V&gt;  K：键的类型；V：值的类型</span><br></pre></td></tr></table></div></figure>

<p>Map集合的特点</p>
<ul>
<li>双列集合,一个键对应一个值</li>
<li>键不可以重复,值可以重复</li>
</ul>

        <h2 id="5-2-基本方法"   >
          <a href="#5-2-基本方法" class="heading-link"><i class="fas fa-link"></i></a><a href="#5-2-基本方法" class="headerlink" title="5.2 基本方法"></a>5.2 基本方法</h2>
      <p>方法介绍</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">V <span class="title function_">put</span><span class="params">(K key,V value)</span>	<span class="comment">//添加元素</span></span><br><span class="line">  </span><br><span class="line">V <span class="title function_">remove</span><span class="params">(Object key)</span>	<span class="comment">//根据键删除键值对元素</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">void</span> <span class="title function_">clear</span><span class="params">()</span>	<span class="comment">//移除所有的键值对元素</span></span><br><span class="line"></span><br><span class="line"><span class="type">boolean</span> <span class="title function_">containsKey</span><span class="params">(Object key)</span>	<span class="comment">//判断集合是否包含指定的键</span></span><br><span class="line">  </span><br><span class="line"><span class="type">boolean</span> <span class="title function_">containsValue</span><span class="params">(Object value)</span>	<span class="comment">//判断集合是否包含指定的值</span></span><br><span class="line">  </span><br><span class="line"><span class="type">boolean</span> <span class="title function_">isEmpty</span><span class="params">()</span>	<span class="comment">//判断集合是否为空</span></span><br><span class="line">  </span><br><span class="line"><span class="type">int</span> <span class="title function_">size</span><span class="params">()</span>	<span class="comment">//集合的长度，也就是集合中键值对的个数</span></span><br></pre></td></tr></table></div></figure>


        <h2 id="5-3-获取方法"   >
          <a href="#5-3-获取方法" class="heading-link"><i class="fas fa-link"></i></a><a href="#5-3-获取方法" class="headerlink" title="5.3 获取方法"></a>5.3 获取方法</h2>
      <p>方法介绍</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">V <span class="title function_">get</span><span class="params">(Object key)</span>	<span class="comment">//根据键获取值</span></span><br><span class="line">  </span><br><span class="line">Set&lt;K&gt; <span class="title function_">keySet</span><span class="params">()</span>	<span class="comment">//获取所有键的集合</span></span><br><span class="line">  </span><br><span class="line">Collection&lt;V&gt; <span class="title function_">values</span><span class="params">()</span>	<span class="comment">//获取所有值的集合</span></span><br><span class="line">  </span><br><span class="line">Set&lt;Map.Entry&lt;K,V&gt;&gt; entrySet()	<span class="comment">//获取所有键值对对象的集合</span></span><br></pre></td></tr></table></div></figure>


        <h2 id="5-4-keySet-方式遍历"   >
          <a href="#5-4-keySet-方式遍历" class="heading-link"><i class="fas fa-link"></i></a><a href="#5-4-keySet-方式遍历" class="headerlink" title="5.4 keySet()方式遍历"></a>5.4 <code>keySet()</code>方式遍历</h2>
      <p>遍历思路：我们刚才存储的元素都是成对出现的，所以我们把Map看成是一个夫妻对的集合</p>
<ul>
<li>把所有的丈夫给集中起来</li>
<li>遍历丈夫的集合，获取到每一个丈夫</li>
<li>根据丈夫去找对应的妻子</li>
</ul>
<p>步骤分析：</p>
<ul>
<li>获取所有键的集合。用keySet()方法实现</li>
<li>遍历键的集合，获取到每一个键。用增强for实现  </li>
<li>根据键去找值。用get(Object key)方法实现</li>
</ul>
<p>代码实现</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">MapDemo01</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">//创建集合对象</span></span><br><span class="line">        Map&lt;String, String&gt; map = <span class="keyword">new</span> <span class="title class_">HashMap</span>&lt;String, String&gt;();</span><br><span class="line"></span><br><span class="line">        <span class="comment">//添加元素</span></span><br><span class="line">        map.put(<span class="string">&quot;张无忌&quot;</span>, <span class="string">&quot;赵敏&quot;</span>);</span><br><span class="line">        map.put(<span class="string">&quot;郭靖&quot;</span>, <span class="string">&quot;黄蓉&quot;</span>);</span><br><span class="line">        map.put(<span class="string">&quot;杨过&quot;</span>, <span class="string">&quot;小龙女&quot;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//获取所有键的集合。用keySet()方法实现</span></span><br><span class="line">        Set&lt;String&gt; keySet = map.keySet();</span><br><span class="line">        <span class="comment">//遍历键的集合，获取到每一个键。用增强for实现</span></span><br><span class="line">        <span class="keyword">for</span> (String key : keySet) &#123;</span><br><span class="line">            <span class="comment">//根据键去找值。用get(Object key)方法实现</span></span><br><span class="line">            <span class="type">String</span> <span class="variable">value</span> <span class="operator">=</span> map.get(key);</span><br><span class="line">            System.out.println(key + <span class="string">&quot;,&quot;</span> + value);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h2 id="5-5-entrySet-方式遍历"   >
          <a href="#5-5-entrySet-方式遍历" class="heading-link"><i class="fas fa-link"></i></a><a href="#5-5-entrySet-方式遍历" class="headerlink" title="5.5 entrySet()方式遍历"></a>5.5 <code>entrySet()</code>方式遍历</h2>
      <p>遍历思路：我们刚才存储的元素都是成对出现的，所以我们把Map看成是一个夫妻对的集合</p>
<ol>
<li>获取所有结婚证的集合</li>
<li>遍历结婚证的集合，得到每一个结婚证</li>
<li>根据结婚证获取丈夫和妻子</li>
</ol>
<p>步骤分析：</p>
<p>1.获取所有键值对对象的集合：<code>Set&lt;Map.Entry&lt;K,V&gt;&gt; entrySet()</code></p>
<p>2.遍历键值对对象的集合，得到每一个键值对对象：用增强for实现，得到每一个<code>Map.Entry</code></p>
<p>3.根据键值对对象获取键和值：用<code>getKey()</code>得到键、用<code>getValue()</code>得到值</p>
<p>代码实现：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">MapDemo02</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">//创建集合对象</span></span><br><span class="line">        Map&lt;String, String&gt; map = <span class="keyword">new</span> <span class="title class_">HashMap</span>&lt;String, String&gt;();</span><br><span class="line"></span><br><span class="line">        <span class="comment">//添加元素</span></span><br><span class="line">        map.put(<span class="string">&quot;张无忌&quot;</span>, <span class="string">&quot;赵敏&quot;</span>);</span><br><span class="line">        map.put(<span class="string">&quot;郭靖&quot;</span>, <span class="string">&quot;黄蓉&quot;</span>);</span><br><span class="line">        map.put(<span class="string">&quot;杨过&quot;</span>, <span class="string">&quot;小龙女&quot;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//获取所有键值对对象的集合</span></span><br><span class="line">        Set&lt;Map.Entry&lt;String, String&gt;&gt; entrySet = map.entrySet();</span><br><span class="line">        <span class="comment">//遍历键值对对象的集合，得到每一个键值对对象</span></span><br><span class="line">        <span class="keyword">for</span> (Map.Entry&lt;String, String&gt; me : entrySet) &#123;</span><br><span class="line">            <span class="comment">//根据键值对对象获取键和值</span></span><br><span class="line">            <span class="type">String</span> <span class="variable">key</span> <span class="operator">=</span> me.getKey();</span><br><span class="line">            <span class="type">String</span> <span class="variable">value</span> <span class="operator">=</span> me.getValue();</span><br><span class="line">            System.out.println(key + <span class="string">&quot;,&quot;</span> + value);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h2 id="5-6-Map集合实现类"   >
          <a href="#5-6-Map集合实现类" class="heading-link"><i class="fas fa-link"></i></a><a href="#5-6-Map集合实现类" class="headerlink" title="5.6 Map集合实现类"></a>5.6 Map集合实现类</h2>
      
        <h3 id="5-6-1-HashMap"   >
          <a href="#5-6-1-HashMap" class="heading-link"><i class="fas fa-link"></i></a><a href="#5-6-1-HashMap" class="headerlink" title="5.6.1 HashMap"></a>5.6.1 HashMap</h3>
      
        <h4 id="5-6-1-1-概述和特点"   >
          <a href="#5-6-1-1-概述和特点" class="heading-link"><i class="fas fa-link"></i></a><a href="#5-6-1-1-概述和特点" class="headerlink" title="5.6.1.1 概述和特点"></a>5.6.1.1 概述和特点</h4>
      <ul>
<li>HashMap底层是哈希表结构的</li>
<li>依赖<code>hashCode()</code>和<code>equals()</code>保证键的唯一</li>
<li>如果键要存储的是自定义对象，需要重写<code>hashCode()</code>和<code>equals()</code></li>
</ul>

        <h4 id="5-6-1-2-应用案例"   >
          <a href="#5-6-1-2-应用案例" class="heading-link"><i class="fas fa-link"></i></a><a href="#5-6-1-2-应用案例" class="headerlink" title="5.6.1.2 应用案例"></a>5.6.1.2 应用案例</h4>
      <p>案例需求：</p>
<ul>
<li>创建一个HashMap集合，键是学生对象(Student)，值是居住地 (String)。存储多个元素，并遍历。</li>
<li>要求保证键的唯一性：如果学生对象的成员变量值相同，我们就认为是同一个对象</li>
</ul>
<p>代码实现：</p>
<p>学生类</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Student</span> &#123;</span><br><span class="line">    <span class="keyword">private</span> String name;</span><br><span class="line">    <span class="keyword">private</span> <span class="type">int</span> age;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="title function_">Student</span><span class="params">()</span> &#123;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="title function_">Student</span><span class="params">(String name, <span class="type">int</span> age)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.name = name;</span><br><span class="line">        <span class="built_in">this</span>.age = age;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> String <span class="title function_">getName</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">setName</span><span class="params">(String name)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.name = name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="type">int</span> <span class="title function_">getAge</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> age;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">setAge</span><span class="params">(<span class="type">int</span> age)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.age = age;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> <span class="type">boolean</span> <span class="title function_">equals</span><span class="params">(Object o)</span> &#123;</span><br><span class="line">        <span class="keyword">if</span> (<span class="built_in">this</span> == o) <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">        <span class="keyword">if</span> (o == <span class="literal">null</span> || getClass() != o.getClass()) <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line"></span><br><span class="line">        <span class="type">Student</span> <span class="variable">student</span> <span class="operator">=</span> (Student) o;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (age != student.age) <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">        <span class="keyword">return</span> name != <span class="literal">null</span> ? name.equals(student.name) : student.name == <span class="literal">null</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> <span class="type">int</span> <span class="title function_">hashCode</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="type">int</span> <span class="variable">result</span> <span class="operator">=</span> name != <span class="literal">null</span> ? name.hashCode() : <span class="number">0</span>;</span><br><span class="line">        result = <span class="number">31</span> * result + age;</span><br><span class="line">        <span class="keyword">return</span> result;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

<p>测试类</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">HashMapDemo</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">//创建HashMap集合对象</span></span><br><span class="line">        HashMap&lt;Student, String&gt; hm = <span class="keyword">new</span> <span class="title class_">HashMap</span>&lt;Student, String&gt;();</span><br><span class="line"></span><br><span class="line">        <span class="comment">//创建学生对象</span></span><br><span class="line">        <span class="type">Student</span> <span class="variable">s1</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Student</span>(<span class="string">&quot;林青霞&quot;</span>, <span class="number">30</span>);</span><br><span class="line">        <span class="type">Student</span> <span class="variable">s2</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Student</span>(<span class="string">&quot;张曼玉&quot;</span>, <span class="number">35</span>);</span><br><span class="line">        <span class="type">Student</span> <span class="variable">s3</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Student</span>(<span class="string">&quot;王祖贤&quot;</span>, <span class="number">33</span>);</span><br><span class="line">        <span class="type">Student</span> <span class="variable">s4</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Student</span>(<span class="string">&quot;王祖贤&quot;</span>, <span class="number">33</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//把学生添加到集合</span></span><br><span class="line">        hm.put(s1, <span class="string">&quot;西安&quot;</span>);</span><br><span class="line">        hm.put(s2, <span class="string">&quot;武汉&quot;</span>);</span><br><span class="line">        hm.put(s3, <span class="string">&quot;郑州&quot;</span>);</span><br><span class="line">        hm.put(s4, <span class="string">&quot;北京&quot;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//遍历集合</span></span><br><span class="line">        Set&lt;Student&gt; keySet = hm.keySet();</span><br><span class="line">        <span class="keyword">for</span> (Student key : keySet) &#123;</span><br><span class="line">            <span class="type">String</span> <span class="variable">value</span> <span class="operator">=</span> hm.get(key);</span><br><span class="line">            System.out.println(key.getName() + <span class="string">&quot;,&quot;</span> + key.getAge() + <span class="string">&quot;,&quot;</span> + value);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h3 id="5-6-2-TreeMap"   >
          <a href="#5-6-2-TreeMap" class="heading-link"><i class="fas fa-link"></i></a><a href="#5-6-2-TreeMap" class="headerlink" title="5.6.2 TreeMap"></a>5.6.2 TreeMap</h3>
      
        <h4 id="5-6-2-1-概述和特点"   >
          <a href="#5-6-2-1-概述和特点" class="heading-link"><i class="fas fa-link"></i></a><a href="#5-6-2-1-概述和特点" class="headerlink" title="5.6.2.1 概述和特点"></a>5.6.2.1 概述和特点</h4>
      <ul>
<li>TreeMap底层是红黑树结构</li>
<li>依赖自然排序或者比较器排序,对键进行排序</li>
<li>如果键存储的是自定义对象,需要实现Comparable接口或者在创建TreeMap对象时候给出比较器排序规则</li>
</ul>

        <h4 id="5-6-2-2-应用案例"   >
          <a href="#5-6-2-2-应用案例" class="heading-link"><i class="fas fa-link"></i></a><a href="#5-6-2-2-应用案例" class="headerlink" title="5.6.2.2 应用案例"></a>5.6.2.2 应用案例</h4>
      <p>案例需求：</p>
<ul>
<li>创建一个TreeMap集合,键是学生对象(Student),值是籍贯(String),学生属性姓名和年龄,按照年龄进行排序并遍历</li>
<li>要求按照学生的年龄进行排序,如果年龄相同则按照姓名进行排序</li>
</ul>
<p>代码实现：</p>
<p>学生类</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Student</span> <span class="keyword">implements</span> <span class="title class_">Comparable</span>&lt;Student&gt;&#123;</span><br><span class="line">    <span class="keyword">private</span> String name;</span><br><span class="line">    <span class="keyword">private</span> <span class="type">int</span> age;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="title function_">Student</span><span class="params">()</span> &#123;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="title function_">Student</span><span class="params">(String name, <span class="type">int</span> age)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.name = name;</span><br><span class="line">        <span class="built_in">this</span>.age = age;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> String <span class="title function_">getName</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">setName</span><span class="params">(String name)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.name = name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="type">int</span> <span class="title function_">getAge</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> age;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">setAge</span><span class="params">(<span class="type">int</span> age)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.age = age;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> String <span class="title function_">toString</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">&quot;Student&#123;&quot;</span> +</span><br><span class="line">                <span class="string">&quot;name=&#x27;&quot;</span> + name + <span class="string">&#x27;\&#x27;&#x27;</span> +</span><br><span class="line">                <span class="string">&quot;, age=&quot;</span> + age +</span><br><span class="line">                <span class="string">&#x27;&#125;&#x27;</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> <span class="type">int</span> <span class="title function_">compareTo</span><span class="params">(Student o)</span> &#123;</span><br><span class="line">        <span class="comment">//按照年龄进行排序</span></span><br><span class="line">        <span class="type">int</span> <span class="variable">result</span> <span class="operator">=</span> o.getAge() - <span class="built_in">this</span>.getAge();</span><br><span class="line">        <span class="comment">//次要条件，按照姓名排序。</span></span><br><span class="line">        result = result == <span class="number">0</span> ? o.getName().compareTo(<span class="built_in">this</span>.getName()) : result;</span><br><span class="line">        <span class="keyword">return</span> result;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

<p>测试类</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Test1</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">      	<span class="comment">// 创建TreeMap集合对象</span></span><br><span class="line">        TreeMap&lt;Student,String&gt; tm = <span class="keyword">new</span> <span class="title class_">TreeMap</span>&lt;&gt;();</span><br><span class="line">      </span><br><span class="line">		<span class="comment">// 创建学生对象</span></span><br><span class="line">        <span class="type">Student</span> <span class="variable">s1</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Student</span>(<span class="string">&quot;xiaohei&quot;</span>,<span class="number">23</span>);</span><br><span class="line">        <span class="type">Student</span> <span class="variable">s2</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Student</span>(<span class="string">&quot;dapang&quot;</span>,<span class="number">22</span>);</span><br><span class="line">        <span class="type">Student</span> <span class="variable">s3</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Student</span>(<span class="string">&quot;xiaomei&quot;</span>,<span class="number">22</span>);</span><br><span class="line">      </span><br><span class="line">		<span class="comment">// 将学生对象添加到TreeMap集合中</span></span><br><span class="line">        tm.put(s1,<span class="string">&quot;江苏&quot;</span>);</span><br><span class="line">        tm.put(s2,<span class="string">&quot;北京&quot;</span>);</span><br><span class="line">        tm.put(s3,<span class="string">&quot;天津&quot;</span>);</span><br><span class="line">      </span><br><span class="line">		<span class="comment">// 遍历TreeMap集合,打印每个学生的信息</span></span><br><span class="line">        tm.forEach(</span><br><span class="line">                (Student key, String value)-&gt;&#123;</span><br><span class="line">                    System.out.println(key + <span class="string">&quot;---&quot;</span> + value);</span><br><span class="line">                &#125;</span><br><span class="line">        );</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h1 id="6-可变参数"   >
          <a href="#6-可变参数" class="heading-link"><i class="fas fa-link"></i></a><a href="#6-可变参数" class="headerlink" title="6.可变参数"></a>6.可变参数</h1>
      
        <h2 id="6-1-可变参数"   >
          <a href="#6-1-可变参数" class="heading-link"><i class="fas fa-link"></i></a><a href="#6-1-可变参数" class="headerlink" title="6.1 可变参数"></a>6.1 可变参数</h2>
      
        <h3 id="6-1-1-可变参数介绍"   >
          <a href="#6-1-1-可变参数介绍" class="heading-link"><i class="fas fa-link"></i></a><a href="#6-1-1-可变参数介绍" class="headerlink" title="6.1.1 可变参数介绍"></a>6.1.1 可变参数介绍</h3>
      <ul>
<li>可变参数又称参数个数可变，用作方法的形参出现，那么方法参数个数就是可变的了</li>
<li>方法的参数类型已经确定,个数不确定,我们可以使用可变参数</li>
</ul>

        <h3 id="6-1-2-可变参数定义格式"   >
          <a href="#6-1-2-可变参数定义格式" class="heading-link"><i class="fas fa-link"></i></a><a href="#6-1-2-可变参数定义格式" class="headerlink" title="6.1.2 可变参数定义格式"></a>6.1.2 可变参数定义格式</h3>
      <figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">修饰符 返回值类型 方法名(数据类型… 变量名) &#123;  &#125;</span><br></pre></td></tr></table></div></figure>


        <h3 id="6-1-3-可变参数的注意事项"   >
          <a href="#6-1-3-可变参数的注意事项" class="heading-link"><i class="fas fa-link"></i></a><a href="#6-1-3-可变参数的注意事项" class="headerlink" title="6.1.3 可变参数的注意事项"></a>6.1.3 可变参数的注意事项</h3>
      <ul>
<li>这里的变量其实是一个数组</li>
<li>如果一个方法有多个参数，包含可变参数，可变参数要放在最后</li>
</ul>

        <h3 id="6-1-4-可变参数的基本使用"   >
          <a href="#6-1-4-可变参数的基本使用" class="heading-link"><i class="fas fa-link"></i></a><a href="#6-1-4-可变参数的基本使用" class="headerlink" title="6.1.4 可变参数的基本使用"></a>6.1.4 可变参数的基本使用</h3>
      <figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">ArgsDemo01</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        System.out.println(sum(<span class="number">10</span>, <span class="number">20</span>));</span><br><span class="line">        System.out.println(sum(<span class="number">10</span>, <span class="number">20</span>, <span class="number">30</span>));</span><br><span class="line">        System.out.println(sum(<span class="number">10</span>, <span class="number">20</span>, <span class="number">30</span>, <span class="number">40</span>));</span><br><span class="line"></span><br><span class="line">        System.out.println(sum(<span class="number">10</span>,<span class="number">20</span>,<span class="number">30</span>,<span class="number">40</span>,<span class="number">50</span>));</span><br><span class="line">        System.out.println(sum(<span class="number">10</span>,<span class="number">20</span>,<span class="number">30</span>,<span class="number">40</span>,<span class="number">50</span>,<span class="number">60</span>));</span><br><span class="line">        System.out.println(sum(<span class="number">10</span>,<span class="number">20</span>,<span class="number">30</span>,<span class="number">40</span>,<span class="number">50</span>,<span class="number">60</span>,<span class="number">70</span>));</span><br><span class="line">        System.out.println(sum(<span class="number">10</span>,<span class="number">20</span>,<span class="number">30</span>,<span class="number">40</span>,<span class="number">50</span>,<span class="number">60</span>,<span class="number">70</span>,<span class="number">80</span>,<span class="number">90</span>,<span class="number">100</span>));</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//    public static int sum(int b,int... a) &#123;</span></span><br><span class="line"><span class="comment">//        return 0;</span></span><br><span class="line"><span class="comment">//    &#125;</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="type">int</span> <span class="title function_">sum</span><span class="params">(<span class="type">int</span>... a)</span> &#123;</span><br><span class="line">        <span class="type">int</span> <span class="variable">sum</span> <span class="operator">=</span> <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">for</span>(<span class="type">int</span> i : a) &#123;</span><br><span class="line">            sum += i;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> sum;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h2 id="6-2-创建不可变集合"   >
          <a href="#6-2-创建不可变集合" class="heading-link"><i class="fas fa-link"></i></a><a href="#6-2-创建不可变集合" class="headerlink" title="6.2 创建不可变集合"></a>6.2 创建不可变集合</h2>
      <p>方法介绍：</p>
<ul>
<li>在List、Set、Map接口中,都存在of方法,可以创建一个不可变的集合<ul>
<li>这个集合不能添加,不能删除,不能修改</li>
<li>但是可以结合集合的带参构造,实现集合的批量添加</li>
</ul>
</li>
<li>在Map接口中,还有一个ofEntries方法可以提高代码的阅读性<ul>
<li>首先会把键值对封装成一个Entry对象,再把这个Entry对象添加到集合当中</li>
</ul>
</li>
</ul>
<p>示例代码</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">MyVariableParameter4</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">// static &lt;E&gt;  List&lt;E&gt;  of(E…elements)  创建一个具有指定元素的List集合对象</span></span><br><span class="line">        <span class="comment">//static &lt;E&gt;  Set&lt;E&gt;  of(E…elements)    创建一个具有指定元素的Set集合对象</span></span><br><span class="line">        <span class="comment">//static &lt;K , V&gt;   Map&lt;K，V&gt;  of(E…elements) 创建一个具有指定元素的Map集合对象</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//method1();</span></span><br><span class="line">        <span class="comment">//method2();</span></span><br><span class="line">        <span class="comment">//method3();</span></span><br><span class="line">        <span class="comment">//method4();</span></span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">method4</span><span class="params">()</span> &#123;</span><br><span class="line">        Map&lt;String, String&gt; map = Map.ofEntries(</span><br><span class="line">                Map.entry(<span class="string">&quot;zhangsan&quot;</span>, <span class="string">&quot;江苏&quot;</span>),</span><br><span class="line">                Map.entry(<span class="string">&quot;lisi&quot;</span>, <span class="string">&quot;北京&quot;</span>));</span><br><span class="line">        System.out.println(map);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">method3</span><span class="params">()</span> &#123;</span><br><span class="line">        Map&lt;String, String&gt; map = Map.of(<span class="string">&quot;zhangsan&quot;</span>, <span class="string">&quot;江苏&quot;</span>, <span class="string">&quot;lisi&quot;</span>, <span class="string">&quot;北京&quot;</span>, <span class="string">&quot;wangwu&quot;</span>, <span class="string">&quot;天津&quot;</span>);</span><br><span class="line">        System.out.println(map);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">method2</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="comment">//传递的参数当中，不能存在重复的元素。</span></span><br><span class="line">        Set&lt;String&gt; set = Set.of(<span class="string">&quot;a&quot;</span>, <span class="string">&quot;b&quot;</span>, <span class="string">&quot;c&quot;</span>, <span class="string">&quot;d&quot;</span>,<span class="string">&quot;a&quot;</span>);</span><br><span class="line">        System.out.println(set);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">method1</span><span class="params">()</span> &#123;</span><br><span class="line">        List&lt;String&gt; list = List.of(<span class="string">&quot;a&quot;</span>, <span class="string">&quot;b&quot;</span>, <span class="string">&quot;c&quot;</span>, <span class="string">&quot;d&quot;</span>);</span><br><span class="line">        System.out.println(list);</span><br><span class="line">        <span class="comment">//list.add(&quot;Q&quot;);</span></span><br><span class="line">        <span class="comment">//list.remove(&quot;a&quot;);</span></span><br><span class="line">        <span class="comment">//list.set(0,&quot;A&quot;);</span></span><br><span class="line">        <span class="comment">//System.out.println(list);</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//        ArrayList&lt;String&gt; list2 = new ArrayList&lt;&gt;();</span></span><br><span class="line"><span class="comment">//        list2.add(&quot;aaa&quot;);</span></span><br><span class="line"><span class="comment">//        list2.add(&quot;aaa&quot;);</span></span><br><span class="line"><span class="comment">//        list2.add(&quot;aaa&quot;);</span></span><br><span class="line"><span class="comment">//        list2.add(&quot;aaa&quot;);</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//集合的批量添加。</span></span><br><span class="line">        <span class="comment">//首先是通过调用List.of方法来创建一个不可变的集合，of方法的形参就是一个可变参数。</span></span><br><span class="line">        <span class="comment">//再创建一个ArrayList集合，并把这个不可变的集合中所有的数据，都添加到ArrayList中。</span></span><br><span class="line">        ArrayList&lt;String&gt; list3 = <span class="keyword">new</span> <span class="title class_">ArrayList</span>&lt;&gt;(List.of(<span class="string">&quot;a&quot;</span>, <span class="string">&quot;b&quot;</span>, <span class="string">&quot;c&quot;</span>, <span class="string">&quot;d&quot;</span>));</span><br><span class="line">        System.out.println(list3);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>
</div><footer class="post-footer"><div class="post-ending ending"><div class="ending__text">------ END ------</div></div><div class="post-copyright copyright"><div class="copyright-author"><span class="copyright-author__name">Author: </span><span class="copyright-author__value"><a href="http://bujiuzhi.gitee.io">不久</a></span></div><div class="copyright-link"><span class="copyright-link__name">Link: </span><span class="copyright-link__value"><a href="http://bujiuzhi.gitee.io/2022/02/16/4.%E9%9B%86%E5%90%88/">http://bujiuzhi.gitee.io/2022/02/16/4.%E9%9B%86%E5%90%88/</a></span></div><div class="copyright-notice"><span class="copyright-notice__name">Copyright: </span><span class="copyright-notice__value">All articles in this blog are licensed under <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.en" rel="external nofollow" target="_blank">BY-NC-SA</a> unless stating additionally</span></div></div><div class="post-tags"><span class="post-tags-item"><span class="post-tags-item__icon"><i class="fas fa-tag"></i></span><a class="post-tags-item__link" href="http://bujiuzhi.gitee.io/tags/%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/">学习笔记</a></span></div><div class="post-reward reward"><div class="reward-button">Buy me a coffee</div><div class="reward-qrcode"><span class="reward-qrcode-alipay"><img class="reward-qrcode-alipay__img" src="/images/alipay.jpg"><div class="reward-qrcode-alipay__text">Alipay</div></span><span class="reward-qrcode-wechat"><img class="reward-qrcode-wechat__img" src="/images/weixin.png"><div class="reward-qrcode-wechat__text">Wechat</div></span></div></div><nav class="post-paginator paginator"><div class="paginator-prev"><a class="paginator-prev__link" href="/2022/09/14/5.%E6%B5%81/"><span class="paginator-prev__icon"><i class="fas fa-angle-left"></i></span><span class="paginator-prev__text">5.流</span></a></div><div class="paginator-next"><a class="paginator-next__link" href="/2022/02/13/3.%E5%B8%B8%E7%94%A8API/"><span class="paginator-prev__text">3.常用API</span><span class="paginator-next__icon"><i class="fas fa-angle-right"></i></span></a></div></nav></footer></div></div><div class="comments" id="comments"><div id="valine-container"></div></div></div><div class="sidebar-wrap" id="sidebar-wrap"><aside class="sidebar" id="sidebar"><div class="sidebar-nav"><span class="sidebar-nav-toc current">Catalog</span><span class="sidebar-nav-ov">Overview</span></div><section class="sidebar-toc"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#1-Collection%E9%9B%86%E5%90%88"><span class="toc-text">
          1.Collection集合</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#1-1%E6%95%B0%E7%BB%84%E5%92%8C%E9%9B%86%E5%90%88%E7%9A%84%E5%8C%BA%E5%88%AB"><span class="toc-text">
          1.1数组和集合的区别</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#1-2-%E9%9B%86%E5%90%88%E7%B1%BB%E4%BD%93%E7%B3%BB%E7%BB%93%E6%9E%84"><span class="toc-text">
          1.2 集合类体系结构</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#1-3-%E6%A6%82%E8%BF%B0%E5%92%8C%E4%BD%BF%E7%94%A8"><span class="toc-text">
          1.3 概述和使用</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1-3-1-Collection%E9%9B%86%E5%90%88%E6%A6%82%E8%BF%B0"><span class="toc-text">
          1.3.1 Collection集合概述</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1-3-2-%E5%88%9B%E5%BB%BACollection%E9%9B%86%E5%90%88%E7%9A%84%E5%AF%B9%E8%B1%A1"><span class="toc-text">
          1.3.2 创建Collection集合的对象</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1-3-3-Collection%E9%9B%86%E5%90%88%E5%B8%B8%E7%94%A8%E6%96%B9%E6%B3%95"><span class="toc-text">
          1.3.3 Collection集合常用方法</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#1-4-Collection%E9%9B%86%E5%90%88%E7%9A%84%E9%81%8D%E5%8E%86"><span class="toc-text">
          1.4 Collection集合的遍历</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1-4-1-%E8%BF%AD%E4%BB%A3%E5%99%A8"><span class="toc-text">
          1.4.1 迭代器</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#1-4-1-1-%E8%BF%AD%E4%BB%A3%E5%99%A8%E4%BB%8B%E7%BB%8D"><span class="toc-text">
          1.4.1.1 迭代器介绍</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#1-4-1-2-Iterator%E4%B8%AD%E7%9A%84%E5%B8%B8%E7%94%A8%E6%96%B9%E6%B3%95"><span class="toc-text">
          1.4.1.2 Iterator中的常用方法</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#1-4-1-3-Collection%E9%9B%86%E5%90%88%E7%9A%84%E9%81%8D%E5%8E%86"><span class="toc-text">
          1.4.1.3 Collection集合的遍历</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#1-4-1-4-%E8%BF%AD%E4%BB%A3%E5%99%A8%E4%B8%AD%E5%88%A0%E9%99%A4%E7%9A%84%E6%96%B9%E6%B3%95"><span class="toc-text">
          1.4.1.4 迭代器中删除的方法</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1-4-2-%E5%A2%9E%E5%BC%BAfor%E5%BE%AA%E7%8E%AF"><span class="toc-text">
          1.4.2 增强for循环</span></a></li></ol></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#2-%E6%B3%9B%E5%9E%8B"><span class="toc-text">
          2.泛型</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#2-1-%E6%B3%9B%E5%9E%8B%E6%A6%82%E8%BF%B0"><span class="toc-text">
          2.1 泛型概述</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-2-%E6%B3%9B%E5%9E%8B%E7%B1%BB"><span class="toc-text">
          2.2 泛型类</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-3-%E6%B3%9B%E5%9E%8B%E6%96%B9%E6%B3%95"><span class="toc-text">
          2.3 泛型方法</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-4-%E6%B3%9B%E5%9E%8B%E6%8E%A5%E5%8F%A3"><span class="toc-text">
          2.4 泛型接口</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-5-%E7%B1%BB%E5%9E%8B%E9%80%9A%E9%85%8D%E7%AC%A6"><span class="toc-text">
          2.5 类型通配符</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#3-List%E9%9B%86%E5%90%88"><span class="toc-text">
          3.List集合</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#3-1-%E6%A6%82%E8%BF%B0%E5%92%8C%E7%89%B9%E7%82%B9"><span class="toc-text">
          3.1 概述和特点</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-2-List%E9%9B%86%E5%90%88%E7%9A%84%E7%89%B9%E6%9C%89%E6%96%B9%E6%B3%95"><span class="toc-text">
          3.2 List集合的特有方法</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-1-List%E9%9B%86%E5%90%88%E7%9A%84%E5%AE%9E%E7%8E%B0%E7%B1%BB"><span class="toc-text">
          3.1 List集合的实现类</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#3-1-1-List%E9%9B%86%E5%90%88%E5%AD%90%E7%B1%BB%E7%9A%84%E7%89%B9%E7%82%B9"><span class="toc-text">
          3.1.1 List集合子类的特点</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-1-2-ArrayList"><span class="toc-text">
          3.1.2 ArrayList</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#3-1-2-1-%E6%9E%84%E9%80%A0%E6%96%B9%E6%B3%95%E5%92%8C%E6%B7%BB%E5%8A%A0%E6%96%B9%E6%B3%95"><span class="toc-text">
          3.1.2.1 构造方法和添加方法</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-1-2-2-%E5%B8%B8%E7%94%A8%E6%96%B9%E6%B3%95"><span class="toc-text">
          3.1.2.2 常用方法</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-1-3-LinkedList"><span class="toc-text">
          3.1.3 LinkedList</span></a></li></ol></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#4-Set%E9%9B%86%E5%90%88"><span class="toc-text">
          4.Set集合</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#4-1-%E6%A6%82%E8%BF%B0%E5%92%8C%E7%89%B9%E7%82%B9"><span class="toc-text">
          4.1 概述和特点</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-2-Set%E9%9B%86%E5%90%88%E7%9A%84%E4%BD%BF%E7%94%A8"><span class="toc-text">
          4.2 Set集合的使用</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-3-Set%E9%9B%86%E5%90%88%E5%AE%9E%E7%8E%B0%E7%B1%BB"><span class="toc-text">
          4.3 Set集合实现类</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#4-3-1-TreeSet"><span class="toc-text">
          4.3.1 TreeSet</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#4-3-1-1%E6%A6%82%E8%BF%B0%E5%92%8C%E7%89%B9%E7%82%B9"><span class="toc-text">
          4.3.1.1概述和特点</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#4-3-1-2-%E9%9B%86%E5%90%88%E5%9F%BA%E6%9C%AC%E4%BD%BF%E7%94%A8"><span class="toc-text">
          4.3.1.2 集合基本使用</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#4-3-1-3-%E8%87%AA%E7%84%B6%E6%8E%92%E5%BA%8FComparable%E7%9A%84%E4%BD%BF%E7%94%A8"><span class="toc-text">
          4.3.1.3 自然排序Comparable的使用</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#4-3-1-4-%E6%AF%94%E8%BE%83%E5%99%A8%E6%8E%92%E5%BA%8FComparator%E7%9A%84%E4%BD%BF%E7%94%A8"><span class="toc-text">
          4.3.1.4 比较器排序Comparator的使用</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#4-3-1-5-%E4%B8%A4%E7%A7%8D%E6%AF%94%E8%BE%83%E6%96%B9%E5%BC%8F%E6%80%BB%E7%BB%93"><span class="toc-text">
          4.3.1.5 两种比较方式总结</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-3-2-HashSet"><span class="toc-text">
          4.3.2 HashSet</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#4-3-2-1-%E6%A6%82%E8%BF%B0%E5%92%8C%E7%89%B9%E7%82%B9"><span class="toc-text">
          4.3.2.1 概述和特点</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#4-3-2-2-%E5%9F%BA%E6%9C%AC%E5%BA%94%E7%94%A8"><span class="toc-text">
          4.3.2.2 基本应用</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#4-3-2-3-%E5%93%88%E5%B8%8C%E5%80%BC"><span class="toc-text">
          4.3.2.3 哈希值</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#4-3-2-4-%E5%93%88%E5%B8%8C%E8%A1%A8%E7%BB%93%E6%9E%84"><span class="toc-text">
          4.3.2.4 哈希表结构</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#4-3-2-5-HashSet%E9%9B%86%E5%90%88%E5%AD%98%E5%82%A8%E5%AD%A6%E7%94%9F%E5%AF%B9%E8%B1%A1%E5%B9%B6%E9%81%8D%E5%8E%86"><span class="toc-text">
          4.3.2.5 HashSet集合存储学生对象并遍历</span></a></li></ol></li></ol></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#5-Map%E9%9B%86%E5%90%88"><span class="toc-text">
          5.Map集合</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#5-1-%E6%A6%82%E8%BF%B0%E5%92%8C%E7%89%B9%E7%82%B9"><span class="toc-text">
          5.1 概述和特点</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#5-2-%E5%9F%BA%E6%9C%AC%E6%96%B9%E6%B3%95"><span class="toc-text">
          5.2 基本方法</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#5-3-%E8%8E%B7%E5%8F%96%E6%96%B9%E6%B3%95"><span class="toc-text">
          5.3 获取方法</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#5-4-keySet-%E6%96%B9%E5%BC%8F%E9%81%8D%E5%8E%86"><span class="toc-text">
          5.4 keySet()方式遍历</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#5-5-entrySet-%E6%96%B9%E5%BC%8F%E9%81%8D%E5%8E%86"><span class="toc-text">
          5.5 entrySet()方式遍历</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#5-6-Map%E9%9B%86%E5%90%88%E5%AE%9E%E7%8E%B0%E7%B1%BB"><span class="toc-text">
          5.6 Map集合实现类</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#5-6-1-HashMap"><span class="toc-text">
          5.6.1 HashMap</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#5-6-1-1-%E6%A6%82%E8%BF%B0%E5%92%8C%E7%89%B9%E7%82%B9"><span class="toc-text">
          5.6.1.1 概述和特点</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#5-6-1-2-%E5%BA%94%E7%94%A8%E6%A1%88%E4%BE%8B"><span class="toc-text">
          5.6.1.2 应用案例</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#5-6-2-TreeMap"><span class="toc-text">
          5.6.2 TreeMap</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#5-6-2-1-%E6%A6%82%E8%BF%B0%E5%92%8C%E7%89%B9%E7%82%B9"><span class="toc-text">
          5.6.2.1 概述和特点</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#5-6-2-2-%E5%BA%94%E7%94%A8%E6%A1%88%E4%BE%8B"><span class="toc-text">
          5.6.2.2 应用案例</span></a></li></ol></li></ol></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#6-%E5%8F%AF%E5%8F%98%E5%8F%82%E6%95%B0"><span class="toc-text">
          6.可变参数</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#6-1-%E5%8F%AF%E5%8F%98%E5%8F%82%E6%95%B0"><span class="toc-text">
          6.1 可变参数</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#6-1-1-%E5%8F%AF%E5%8F%98%E5%8F%82%E6%95%B0%E4%BB%8B%E7%BB%8D"><span class="toc-text">
          6.1.1 可变参数介绍</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#6-1-2-%E5%8F%AF%E5%8F%98%E5%8F%82%E6%95%B0%E5%AE%9A%E4%B9%89%E6%A0%BC%E5%BC%8F"><span class="toc-text">
          6.1.2 可变参数定义格式</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#6-1-3-%E5%8F%AF%E5%8F%98%E5%8F%82%E6%95%B0%E7%9A%84%E6%B3%A8%E6%84%8F%E4%BA%8B%E9%A1%B9"><span class="toc-text">
          6.1.3 可变参数的注意事项</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#6-1-4-%E5%8F%AF%E5%8F%98%E5%8F%82%E6%95%B0%E7%9A%84%E5%9F%BA%E6%9C%AC%E4%BD%BF%E7%94%A8"><span class="toc-text">
          6.1.4 可变参数的基本使用</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#6-2-%E5%88%9B%E5%BB%BA%E4%B8%8D%E5%8F%AF%E5%8F%98%E9%9B%86%E5%90%88"><span class="toc-text">
          6.2 创建不可变集合</span></a></li></ol></li></ol></section><!-- ov = overview--><section class="sidebar-ov hide"><div class="sidebar-ov-author"><div class="sidebar-ov-author__avatar"><img class="sidebar-ov-author__avatar_img" src="/images/icons/cute.jpg" alt="avatar"></div><p class="sidebar-ov-author__text">星夜兼程</p></div><div class="sidebar-ov-social"><a class="sidebar-ov-social-item" href="https://gitee.com/bujiuzhi" target="_blank" rel="noopener" data-popover="social.gitee" data-popover-pos="up"><span class="sidebar-ov-social-item__icon">gitee</span></a><a class="sidebar-ov-social-item" href="https://github.com/bujiuzhi" target="_blank" rel="noopener" data-popover="Github" data-popover-pos="up"><span class="sidebar-ov-social-item__icon"><i class="fab fa-github"></i></span></a><a class="sidebar-ov-social-item" href="https://twitter.com/bujiuzhi" target="_blank" rel="noopener" data-popover="Twitter" data-popover-pos="up"><span class="sidebar-ov-social-item__icon"><i class="fab fa-twitter"></i></span></a><a class="sidebar-ov-social-item" href="https://space.bilibili.com/340646348" target="_blank" rel="noopener" data-popover="Youtube" data-popover-pos="up"><span class="sidebar-ov-social-item__icon">bilibili</span></a><a class="sidebar-ov-social-item" href="https://www.zhihu.com/people/bujiuzhi" target="_blank" rel="noopener" data-popover="Zhihu" data-popover-pos="up"><span class="sidebar-ov-social-item__icon">知</span></a></div><div class="sidebar-ov-state"><a class="sidebar-ov-state-item sidebar-ov-state-item--posts" href="/archives/"><div class="sidebar-ov-state-item__count">7</div><div class="sidebar-ov-state-item__name">Archives</div></a><a class="sidebar-ov-state-item sidebar-ov-state-item--categories" href="/categories/"><div class="sidebar-ov-state-item__count">3</div><div class="sidebar-ov-state-item__name">Categories</div></a><a class="sidebar-ov-state-item sidebar-ov-state-item--tags" href="/tags/"><div class="sidebar-ov-state-item__count">5</div><div class="sidebar-ov-state-item__name">Tags</div></a></div><div class="sidebar-ov-cc"><a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.en" target="_blank" rel="noopener" data-popover="Creative Commons" data-popover-pos="up"><img src="/images/cc-by-nc-sa.svg"></a></div></section><div class="sidebar-reading"><div class="sidebar-reading-info"><span class="sidebar-reading-info__text">You have read </span><span class="sidebar-reading-info__num">0</span><span class="sidebar-reading-info__perc">%</span></div><div class="sidebar-reading-line"></div></div></aside></div><div class="clearfix"></div></div></main><footer class="footer" id="footer"><div class="footer-inner"><div><span>Copyright © 2023</span><span class="footer__icon"><i class="fas fa-heart"></i></span><span>不久</span></div><div><span>Powered by <a href="http://hexo.io/" title="Hexo" target="_blank" rel="noopener">Hexo</a></span><span> v6.3.0</span><span class="footer__devider">|</span><span>Theme - <a href="https://github.com/liuyib/hexo-theme-stun/" title="Stun" target="_blank" rel="noopener">Stun</a></span><span> v2.7.0</span></div></div></footer><div class="loading-bar" id="loading-bar"><div class="loading-bar__progress"></div></div><div class="back2top" id="back2top"><span class="back2top__icon"><i class="fas fa-rocket"></i></span></div></div><script src="https://cdn.jsdelivr.net/npm/jquery@v3.4.1/dist/jquery.min.js"></script><script src="https://cdn.jsdelivr.net/npm/velocity-animate@1.5.2/velocity.min.js"></script><script src="https://cdn.jsdelivr.net/npm/velocity-animate@1.5.2/velocity.ui.min.js"></script><script src="https://cdn.jsdelivr.net/npm/canvas-nest.js@1.0.1/dist/canvas-nest.min.js" color="0,0,0" opacity="0.6" count="99" zIndex="-1"></script><script src="https://cdn.jsdelivr.net/npm/leancloud-storage@latest/dist/av-min.js"></script><script src="https://cdn.jsdelivr.net/npm/valine@latest/dist/Valine.min.js"></script><script>function loadValine () {
  var GUEST_INFO = ['nick', 'mail', 'link'];
  var guest_info = 'nick,mail,link';

  guest_info = guest_info.split(',').filter(function(item) {
    return GUEST_INFO.indexOf(item) > -1;
  });
  new Valine({
    el: '#valine-container',
    appId: '5aO7FYnqH2acFvUnyGxifpjn-gzGzoHsz',
    appKey: 'eqj3y9AYWsueEDToYMjl0c5i',
    notify: true,
    verify: true,
    placeholder: 'Just go go',
    avatar: 'mp',
    meta: guest_info,
    pageSize: '10' || 10,
    visitor: false,
    recordIP: false,
    lang: '' || 'zh-cn',
    path: window.location.pathname
  });
}

if (false) {
  loadValine();
} else {
  window.addEventListener('DOMContentLoaded', loadValine, false);
}</script><script src="/js/utils.js?v=2.7.0"></script><script src="/js/stun-boot.js?v=2.7.0"></script><script src="/js/scroll.js?v=2.7.0"></script><script src="/js/header.js?v=2.7.0"></script><script src="/js/sidebar.js?v=2.7.0"></script></body></html>